Hello World,
In this post, we are basically revisiting a previous post we have published some time ago. The post Windows 10 – How to Force Refresh of Desktop Wallpaper (without logoff/logon) was investigating the possibility to refresh/change a desktop wallpaper without the need of a logoff/logon operation. Our previous post was providing a sample PowerShell Script snippet that can perform such action and would provide a good way to automate the full process in case you need to deploy the solution to a large number of computers.
Recently, some readers came back to us and were challenging us and asked us to improve the script. So, we have decided to revisit our previous post, and we have decided to re-write the PowerShell script. The script should be seen as a starting point and should be modified as required to match your scenario requirements…
So, let’s quickly do this
Revisited PowerShell Script : Update-WallPaper.ps1
Overview
In our previous post, we have been providing a small sample script that could be used to update desktop wallpaper without the need of the user to logoff and logon back. This script sample was developed to meet a really specific requirement. Indeed, if a user decide to change the wallpaper, the change will be applied immediately and the new desktop wallpaper will be visible with no need to logoff/logon. In corporate environment where Desktop wallpaper are managed through GPO, if you update the image to be used, the change will only apply if the user perform a logoff/logon action. So, the Update-Wallpaper PowerShell script can be used in this kind of scenario.
The initial script was simply providing an option to reflect changes immediately if a Wallpaper image was pushed through Group Policies. Some users were requesting if it was possible to add some additional parameters in order to change the way the wallpaper image would be displayed on the screen. The previous script was not taking into account the option to center, stretch, fill, Tiling or fit the wallpaper image.
This version of the script is basically overcoming this shortcoming and by specifying some additional parameters to be used with the script, it’s now possible to specify the image path and image preferences. In this way the script is becoming really more flexible and can be used in different scenarios where such requirements is needed
Disclaimer
This script is provided AS-IS. Use this script at your own risk !!!
The Script Explained
The script code is presented at the end of this post.
Using the script is quite straight forward. We have coded the script in order for you to pass some parameters. The following parameters can be passed to the function
- -path mandatory parameter. Specify the location where the desktop wallpaper image to be used is located
- -Style optional Parameter. Possible Value are
-
-
- Centered
- Stretched
- Fill
- Fit
- Span
-
-
- -Tiled optional Parameter. Default Value is NoTiles. Possible Value are
-
-
- Tiles
- NoTiles
-
-
If Tiled switch is set to Tiles, the Style Parameter is ignored and Tile option takes over
How to run it
Warning !
Your Windows might be blocking powershell script that are not signed, you will not be able to simply execute the script from your Powershell and you might end up with the following error
Click on Picture for Better Resolution
To fix this issue, you can either relax your powershell Execution Policy or sign the script. We have coded the script in order for you to pass the parameters and should provide you some flexibility and should be able to run it as long as you execute it accordingly. If you run it from command prompt, you will have to issue the following command
powershell.exe -ExecutionPolicy Bypass -file c:\Script\Update-Wallpaper.ps1 -Path c:\path\yourImage.png -Style Centered Note : Adapt path to reflect your infrastructure
Click on Picture for Better Resolution
You can use this command in your Group policy or group policies and as you can see since we have parameters available, you can target different groups of computer if you would need to
You can also directly running the script from the PowerShell Script Console and again, you will simply need to go to the Update-wallpaper script and specify at minimum path parameter option.
Click on Picture for Better Resolution
Final Notes
Voila ! This is it for this post.
Based on the feedback received from some readers, we have decided to revisit our script that can be used to refresh desktop wallpaper without logging off from the computer. This new version of the script accept some parameters and allows you to fine tune how the desktop wallpaper needs to be configured (tiles, center, Fill, Span….) . We hope you will enjoy this script…
Use this script at your own risk. This script can be used as starting point to a more complex script that could help you achieve your goal.
Till Next Time
See ya
Annex – The Update-Wallpaper Script Code
#--------------------------------------------------------------------------------------------------# # ScriptName : Update-WallPaper.ps1 # # Description : Force a Desktop wallpaper Refresh with no logoff/login needed # # Date : December 2021 # # Written : Griffon # # WebSite :http://www.c-nergy.be - http://www.c-nergy.be/blog # # Version : 2.0 # # History : 2.0 - Adding functions and parameters to cover more scenario # # : 1.0 - Initial Version - Basic Settings # # # # Credits : Unknown (multipe Internet Sources & References) # # # # Notes : We have been using multiple resources from internet and cope snippets in order to come # # with this script...The following link has provided us really good base and info to build # # up the script (https://stackoverflow.com/questions/19989906/ how-to-set-wallpaper-style # # -fill-stretch-according-to-windows-version). # # # # Disclaimer : Script provided AS IS. Use it at your own risk.... # # You can use this script and distribute it as long as credits are kept # # in place and unchanged # # # #--------------------------------------------------------------------------------------------------# <# Credits/ info from https://stackoverflow.com/questions/19989906/how-to-set-wallpaper-style-fill-stretch-according-to-windows-version ' Set the wallpaper style and tile. ' Two registry values are set in the Control Panel\Desktop key. ' TileWallpaper ' 0: The wallpaper picture should not be tiled ' 1: The wallpaper picture should be tiled ' WallpaperStyle ' 0: The image is centered if TileWallpaper=0 or tiled if TileWallpaper=1 ' 2: The image is stretched to fill the screen ' 6: The image is resized to fit the screen while maintaining the aspect ' ratio. (Windows 7 and later) ' 10: The image is resized and cropped to fill the screen while ' maintaining the aspect ratio. (Windows 7 and later) ' 22: Span image #> # Pass Parameters to the Script [CmdletBinding()] param( [Parameter()] [string]$Path, [ValidateSet('Centered', 'Stretched', 'Fill', 'Fit', 'Span')] $Style = 'Fill', [ValidateSet('Tiles','NoTiles')] $Tiled = '0') #---------------------------------------------------# # Hash Table for WallPaper Style Value # #---------------------------------------------------# $Wstyle = @{ 'Centered' = 0 'Stretched' = 2 'Fill' = 10 'Fit' = 6 'Span' = 22 } #-----------------------------------------------------------------# # Hash Table for Tiles Option. Tiles can be set to 1 # # if Wstyle is centered. Otherwise, should be set to 0 # #-----------------------------------------------------------------# $WTile = @{ 'Tiles' = 1 'NoTiles' = 0 } #Main Code $code = @' using System.Runtime.InteropServices; namespace Win32{ public class Wallpaper{ [DllImport("user32.dll", CharSet=CharSet.Auto)] static extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni) ; public static void SetWallpaper(string thePath){ SystemParametersInfo(20,0,thePath,3); } } } '@ if ($error[0].exception -like "*Cannot add type. The type name 'Win32.Wallpaper' already exists.*") { write-host "Win32.Wallpaer assemblies already loaded" write-host "Proceeding" } else { add-type $code } # Code for settings TileStyle and Wallpaper Style Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name wallpaperstyle -Value $Wstyle[$Style] Set-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name TileWallpaper -Value $WTile[$Tiled] #Apply the Change on the system [Win32.Wallpaper]::SetWallpaper($Path)
Thanks for the script. In the script, there needs to be a space between “add-type” and “$code”.
add-type$code
Thank you
Also, the powershell command’s style parameter should be “Centered” instead of “Center”
powershell.exe -ExecutionPolicy Bypass -file c:\Script\Update-Wallpaper.ps1 -Path c:\path\yourImage.png -Style Centered
@Selim,
Thank you for visiting our blog and providing feedback… We have corrected the typo in the blog… Thank you for letting us know..
Till next time
See ya
@Selim,
typo corrected… Well spotted 🙂
Till next time
See ya
Script is not running with Task scheduler. I have tested other powershell scripts and they are working via Task Scheduler.
@Aslam,
Thank you for visiting our blog and providing some feedback. You are not providing enough information here. Maybe you have to provide some screenshots on how you have configured your task. Do you see an error code in your Task scheduler console ? Where are the file located ? Under which account the task is running ? Have you configured the GPO or Local gpo to allow users to run scheduled tasks ?
So, we have tried a basic scheduled task and the task is running fine.. We have used only local files and no shares in our test (because lack of time) but if the user has the proper permissions the script should run also from a share
Hope this help
Till next time
See ya
Thank you for this baseline script.
Is it possible to block the users from being able to manually change the wallpaper via the script or is this something only done with GPO?
Thanks
@Dave,
Thank you for visiting our blog and providing feedback. So, usually, if you want to prevent user to change wallpaper, you would use GPO or you can also use some registry keys
Hope this help
Till next time
See ya