Set lock screen wallpaper #1254
PastLives-GIT
started this conversation in
Custom scripts
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Set-LockScreen.ps1 Script Description
Introduction
Set-LockScreen.ps1is a PowerShell script that sets the specified image file as the lock screen wallpaper for the current Windows user.It calls the
Windows.System.UserProfile.LockScreenAPI from the Windows Runtime (WinRT) and does not require administrator privileges.Features
Parameters
PathUsage
1. Save the script
Save the script content as
Set-LockScreen.ps1.2. Run the script
Option 1: Configure in scripts.yaml
Example:
Replace
<path-to-script>with the actual directory where you saved the script, and<path-to-image>with the full path to your target image.Option 2: Run directly with PowerShell (for testing before configuring in scripts.yaml)
In Windows PowerShell 5.1, run:
Or use a positional parameter directly:
If the execution policy prevents the script from running, use:
Complete Script Source Code
How It Works
Path resolution and validation
Converts the input path to an absolute path using
[System.IO.Path]::GetFullPath()and checks whether the file exists withTest-Path.Format check
Checks whether the file extension is a common image format and warns if it is not in the list.
Load the WinRT helper assembly
Loads the required assembly with
Add-Type -AssemblyName System.Runtime.WindowsRuntimeto handle WinRT asynchronous operations.Define async wait functions
Wait-WinRTAsyncAction: Waits for anIAsyncActionto complete.Wait-WinRTAsyncOperation: Waits for anIAsyncOperation<T>to complete and returns the result.Call the WinRT API
[Windows.Storage.StorageFile]::GetFileFromPathAsync()to obtain aStorageFileobject.[Windows.System.UserProfile.LockScreen]::SetImageFileAsync()to set the file as the lock screen wallpaper.Result feedback
Outputs a success message in green, or detailed error information if the operation fails.
Notes
.jpg,.png, and.bmpare recommended. Other formats may not be supported by the lock screen API.Summary
This script provides a simple and reliable way for users to quickly change the Windows lock screen wallpaper from the command line. It uses the system's native API, avoiding third-party tools or registry modifications, and is suitable for automated deployment or personal use.
Enjoy the pleasure of tinkering!
All reactions