Call Windows Runtime Classes from PowerShell

Is there a way to call Windows Runtime classes (WinRT) (or objects) from a PowerShell script? I know that you can call COM objects, which WinRT classes should be "exposed" as ... but so far my attempts have failed ...

This is my code I'm trying:

$lockscreen = New-Object -comObject Windows.System.UserProfile.LockScreen 

Which gives me the following error:

 New-Object : Retrieving the COM class factory for component with CLSID {00000000-0000-0000-0000-000000000000} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). 

Does anyone know the correct “COM class” that I should use for WinRT classes?

+4
source share
2 answers

Here's something hacked that looks like a job:

 PS> new-object "Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime" new-object : Constructor not found. Cannot find an appropriate constructor for type Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime. At line:1 char:1 + new-object "Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,Con ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand PS> [Windows.System.UserProfile.LockScreen]::OriginalImageFile AbsolutePath : C:/Windows/Web/Screen/img100.png AbsoluteUri : file:///C:/Windows/Web/Screen/img100.png LocalPath : C:\Windows\Web\Screen\img100.png Authority : HostNameType : Basic IsDefaultPort : True IsFile : True IsLoopback : True PathAndQuery : C:/Windows/Web/Screen/img100.png ... 

Note that the first call failed because LockScreen does not have a constructor, but this call does something to pull out the WinRT projection / metadata so that you can now call the static methods / properties in the LockScreen class.

DISCLAIMER: There is no documentation that I can find in this New-Object syntax, so it’s possible that Microsoft can change it, given that it is essentially a “hidden” and possibly not fully developed function.

+4
source

just reference the type to “load” the assembly ...

 [Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime] [Windows.System.UserProfile.LockScreen]::OriginalImageFile 

if you do not want the type to return to your powershell results, then

 $null = [Windows.System.UserProfile.LockScreen,Windows.System.UserProfile,ContentType=WindowsRuntime] 
+1
source

All Articles