Search code examples
windowspowershelllockscreen

Call Windows Runtime Classes from PowerShell - window 8 Lockscreen Switcher


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)

If someone could explain better to me how to work with these .Net objects, or at least the code needed to go from having a file path (ie c:\users\UserName\Wallpapers\newImage.jpg) to setting that file as the lockscreen That would be so awesome. Sorry My programing terminology is pretty week so please forgive me if I'm using the wrong terms.


Solution

  • Judging from the Lack of Responses it looks like working with WinRT Classes must be hard. However I did find a different way to programmatically change the Lock screen in windows 8 using powershell.

    Thanks to Will over on powershell.org I found out that in windows 8/8.1 there is a user Level Lock screen and a system level lock screen. The system level lock screen image can be set in a registry key, and the User level lock screen can be disabled with a registry key, which leaves the system Lockscreen to be the global lock screen. Check out eightforums.com for more details.

    So here is the code that I worked out for setting a random lock-screen.

    $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
    }