This PowerShell script automatically sets a random image from a user-specified folder as the Windows lock screen wallpaper. It uses modern Windows Runtime APIs for robust performance and does not require administrator privileges.
- No Admin Rights Needed: Runs safely under a standard user account.
- Random Image Selection: Automatically picks a random image (
.jpg,.jpeg,.png) from your chosen folder. - Detailed Logging: Creates a log file (
LockScreen_Log.txt) in yourDocumentsfolder. This file records every step of the process, making it easy to troubleshoot any issues. - Path Validation: Checks if the specified image folder exists before attempting to read from it.
- Error Handling: Provides clear error messages in the log file if no images are found or if something goes wrong.
- Modern API Usage: Utilizes the
Windows.System.UserProfile.LockScreenAPI for a reliable and officially supported way to change the lock screen on Windows 10 and 11.
Follow these steps to get the script up and running.
-
Open the script file in a text editor (like Notepad, VS Code, etc.).
-
Locate the following line at the top of the script:
$imageFolder = "Paste the file path containing the image here."
-
Replace
"Paste the file path containing the image here."with the absolute path to the folder where your images are stored.Example:
$imageFolder = "C:\Users\MyUser\Pictures\Wallpapers"
or
$imageFolder = "D:\My Photos\Favorites"
Save the file with a .ps1 extension. A recommended name is Set-RandomLockScreen.ps1.
You can run this script directly without needing administrator rights.
- Right-click the script file (
Set-RandomLockScreen.ps1). - Select "Run with PowerShell".
Alternatively, you can open a standard PowerShell window and run the script from there:
# Navigate to the directory where you saved the script
cd C:\Path\To\Your\Script
# Run the script
.\Set-RandomLockScreen.ps1If you encounter an error stating that scripts are disabled on your system, you may need to change the PowerShell execution policy. You can do this by running the following command in a PowerShell window:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserAnswer Y (Yes) when prompted. This command allows locally saved scripts to run.
The script creates and updates a log file each time it runs. This is very useful for confirming that the script ran successfully or for diagnosing problems.
- Log File Location:
C:\Users\<Your-Username>\Documents\LockScreen_Log.txt - Successful Run: If everything works, the log will show which image was selected and a "SUCCESS" message at the end.
- Errors: If an error occurs (e.g., folder not found, no images), the log will contain detailed error messages that can help you identify the cause.
- OS: Windows 10 / Windows 11
- PowerShell: v5.1 or later
This README was created by gemini-2.5-pro
If you have any questions about this program, please post them in the comments of this gist. I may not be able to reply immediately, but I will respond as soon as I notice them.
Hello!
The script works perfectly. Is there a way to automatically run it at specific intervals or any time you are signed out?
Thanks :)
Edit: nvm, I just found out about task scheduler. Thanks for the script though.