Reputation: 13
Recently I've come across a situation where we need to set the default wallpaper as well as any existing user's wallpaper with PowerShell.
We can set the default wallpaper by replacing the file C:\Windows\Web\Wallpaper\Windows\img0.jpg but I've yet to find a suitable solution for replacing any existing wallpapers.
Some things I've thought about / tried:
Am I completely missing something here? Does anyone have a suggestion for how we could set this up?
Thanks in advance!
Upvotes: 0
Views: 7037
Reputation: 13
After some playing around, here's what I came up with. I'm no PowerShell guru so feel free to throw out some better solutions.
# Get each folder under "Users"
$drive = (Get-Location).Drive.Root
$users = Get-ChildItem "$($drive)Users"
# For each user, load and edit their registry
foreach ( $user in $users ) {
# If this isn't us, load their hive and set the directory
# If this is us, use HKEY_CURRENT_USER
if ( $user.Name -ne $env:username ) {
reg.exe LOAD HKU\Temp "$($drive)Users\$($user.Name)\NTUSER.DAT"
$dir = "Registry::HKEY_USERS\Temp\Control Panel\Desktop"
} else {
$dir = "Registry::HKEY_CURRENT_USER\Control Panel\Desktop"
}
# We don't care about users that don't have this directory
if ( (Test-Path $dir) ) {
# Set the image
Set-ItemProperty -Path $dir -Name "Wallpaper" -value "$($drive)Users\Public\Pictures\Sample Pictures\Tulips.jpg"
# Set the style to stretch
Set-ItemProperty -Path $dir -Name "WallpaperStyle" -value 2
}
# Unload user's hive
if ( $user.Name -ne $env:username ) {
[gc]::Collect()
reg.exe UNLOAD HKU\Temp
}
}
I'd like to thank TheMadTechnician for the GPO suggestion. Under normal circumstances, I'd definitely agree!
Upvotes: 1