Call Windows Runtime Classes from PowerShell

Tagged: , ,

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Will Anderson Will Anderson 1 year, 11 months ago.

  • Author
    Posts
  • #21361
    Profile photo of Jordan Ritz
    Jordan Ritz
    Participant

    I'm trying to make a what I thought would be a simple script to pick a random file from a given directory and set it as the lock-screen image on windows 8. Unfortunately it doesn't look like windows exposes anything directly to powershell to touch the lockscreen. I started out with this question from Stack overflow, call-windows-runtime-classes-from-powershell which looked like he started with the same end goal as I have, but the thread doesn't go into how to actually set the image.

    With a little more searching I found this link How to change Lockscreen using JS in win8 app which got me started down the right path I also found examples for Javascript, C#, C++, and VB on msdn on how to change the lockscreen but I just can't quite figure out how to implement in Powershell.

    Picking a random image is trivial:

     $wallpaper = Get-ChildItem $Path2wallpaper
    $NewWPimage = $wallpaper[(Get-Random -Maximum ($wallpaper.count))].FullName 

    What I'm stuck on is how to actually set the image. Looking at everything I could find I think the code below should work if I knew how to convert $NewWPimage to a .Net stream?

     [Windows.System.UserProfile,Windows.System.UserProfile,ContentType=WindowsRuntime]
    [Windows.System.UserProfile.LockScreen]::SetImageStreamAsync($img1) 

    So two questions:
    1. Is it even possible to do what I'm trying to do with WinRT classes from Powershell.
    2. If ( #1 -eq $true) { Can you point me to some documentation that I'm missing or help me fill in the dots to go from the code examples I have to working PowerShell? }

  • #21391
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    Hey there Jordan,

    You can actually change the lock screen with a regkey setting. I'll whip up a quick script shortly. Here's some reading for you.

    http://www.eightforums.com/tutorials/14485-lock-screen-background-image-set-default-windows-8-a.html

  • #21393
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    Sorry it took so long! I needed to test the code to make sure it was good to go. Here you go:

    $wallpaper = Get-ChildItem $Path2wallpaper
    $NewWPimage = $wallpaper[[Get-Random -Maximum [$wallpaper.count]]].FullName
    $RegPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Personalization"
    
    New-Item -Path $RegPath -Type Directory
    New-ItemProperty -Path $RegPath -Name LockScreenImage -PropertyType String -Value $NewWPimage

    If you want to fire it on a remote computer, you'll of course need to wrap it in an Invoke-Command to do so. Let me know how this works for you!

  • #21417
    Profile photo of Jordan Ritz
    Jordan Ritz
    Participant

    Will,
    That's Perfect, Thanks for the link and code example.

    For anyone coming across this down the road Will's Code will through an error if the Reg key or Property exist already since it uses the New-* cmdlets. Also for my perposes where I wanted to only have 1 lock screen image for the system, I had to add the Registry key to disable User Lock Screens. I've Included my code below for reference encase it can help someone else.

    $wallpaper = Get-ChildItem $Path2wallpaper
    $NewWPimage = $wallpaper[(Get-Random -Maximum ($wallpaper.count))].FullName
    $RegPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Personalization"
    if (Test-Path -Path $RegPath)
    {
        Set-ItemProperty -Path $RegPath -Name LockScreenImage -Value $NewWPimage
    }
    else
    {
        New-Item -Path $RegPath -Type Directory
        New-ItemProperty -Path $RegPath -Name NoChangingLockScreen -PropertyType DWORD -Value 1
        New-ItemProperty -Path $RegPath -Name LockScreenImage -PropertyType String -Value $NewWPimage
    }
    
  • #21418
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    Nicely done Jordan!

    You can also use the -Force parameter to overwrite the key if it exists, should you desire to do so.

You must be logged in to reply to this topic.