Rotate Windows Lock Screen Picture by Powershell

该文章根据 CC-BY-4.0 协议发表,转载请遵循该协议。
本文地址:https://fenying.net/en/post/2024/06/12/rotate-lock-screen-picture-by-powershell/

Another strange problem in Bug 11, the “Slideshow” in the lock screen doesn’t work at all. No matter how you choose the images directory, it always shows two kinds of error messages:

  • The directory you selected cannot be used as the picture directory.
  • You need more than one picture for slideshow

However, if you choose a single image in “Picture” mode, it can display normally.

All right, I can’t stand M$ stupid engineers anymore, so let’s solve this problem with a Powershell script.

The plan is simple, just write a Powershell script that randomly selects an image from a specified directory each time it runs, and then sets it as the lock screen wallpaper.

If that works, we could set up a scheduled task to run this script by interval, and you can achieve the goal of changing the lock screen wallpaper randomly and regularly.

Step #1: Create the Powershell script

Here is the Powershell script that randomly selects an image and sets it as the lock screen wallpaper:

 1$ImageDirectory = $PSScriptRoot # This is the directory where you store the images, I assume it is the same as the directory where the script is located
 2
 3# 1. Get all files in the directory
 4$Files = Get-ChildItem -Path $ImageDirectory
 5
 6# 2. Filter out image files
 7$ImageFiles = $Files | Where-Object { $_.Extension -in ".jpg", ".jpeg", ".png" }
 8
 9# 3. Randomly select an image
10$RandomImage = Get-Random -InputObject $ImageFiles
11
12# 4. The registry path of the lock screen wallpaper
13$Key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP'
14
15# 5. Set the registry item for the lock screen wallpaper
16Set-ItemProperty -Path $Key -Name LockScreenImagePath -Value $RandomImage.FullName

Save this content as a file named set-random-lock-screen.ps1, put it in your image directory, but don’t execute it yet.

This file requires administrator privileges to run, so you need to run it as an administrator later.

Let’s prepare the environment first.

  1. Press Win-R, open the Run dialog
  2. Type regedit to open the registry editor
  3. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP
  4. Make sure PersonalizationCSP registry directory exists (create one if it doesn’t).

Now, let’s execute this script.

  1. Press Win-X, open “Windows Powershell (Administrator)”.
  2. Enter cd "your-image-directory", switch to your image directory
  3. Enter powershell -ExecutionPolicy Bypass -File "set-random-lock-screen.ps1", execute this script.

Done, now press Win-L to lock the screen and see if the lock screen wallpaper has been changed.

Step #2: Set up a scheduled task

Next, let’s set up a scheduled task to run this script every day at midnight.

Open an administrator Powershell terminal and enter the following command:

1$ScriptPath = "C:\Users\Demo\Pictures\LockScreenPictures\set-random-lock-screen.ps1" # This is your script path, please replace it with your actual path
2
3$TaskName = "SetRandomLockScreen" # The name of the scheduled task, you can customize it but keep it unique and don't forget it
4
5$Trigger = New-ScheduledTaskTrigger -Daily -At 12am # Run the task every day at midnight
6
7$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$ScriptPath`""
8
9Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -Action $Action -RunLevel Highest -User "SYSTEM"

Great, here we go. Now, the lock screen wallpaper will be changed randomly every day at midnight.

And, of course, you can change it to run every time you start up, or every few hours, depending on your needs.

For example, to run every 30 minutes, let me show you, just continue executing the following command in the terminal:

1$TaskName = "SetRandomLockScreen" # The name of the scheduled task you created before
2
3$Task = Get-ScheduledTask -TaskName $TaskName
4$Task.Triggers.Repetition.Interval = 'PT30M'        # Run every 30 minutes
5$Task.Triggers.Repetition.Duration = 'PT23H30M'     # Last for 23 hours and 30 minutes
6
7$Task | Set-ScheduledTask

All done. Now, the lock screen wallpaper will be changed every 30 minutes.

This solution also has another benefit: sometimes the slideshow will combine multiple images, which is not beautiful and affects our appreciation of the images. Using the single image mode will only display one image at a time. 😄

comments powered by Disqus

Translations: