I created a script to change the wallpaper on Windows laptops. When tested locally via PowerShell, it works as expected, successfully changing the wallpaper. However, deploying the same script via Intune only downloads the image to the specified path, but the wallpaper does not update.
# Define the URL of the wallpaper image and the destination folder
$WallpaperURL = "https://path\to\wallpaper"
$DestinationFolder = "C:\ProgramData\Wallpaper"
$WallpaperPath = "$DestinationFolder\wallpaper.jpg"
# Ensure the destination folder exists
if (-not (Test-Path -Path $DestinationFolder)) {
New-Item -Path $DestinationFolder -ItemType Directory -Force
}
# Download the wallpaper image securely
try {
Write-Host "Downloading wallpaper from $WallpaperURL..."
Invoke-WebRequest -Uri $WallpaperURL -OutFile $WallpaperPath -TimeoutSec 30
Write-Host "Wallpaper downloaded successfully."
} catch {
Write-Host "Failed to download the wallpaper: $_"
exit 1
}
# Set the wallpaper registry key
try {
Write-Host "Setting the wallpaper..."
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop\" -Name "Wallpaper" -Value $WallpaperPath
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop\" -Name "WallpaperStyle" -Value 2 # 2 = Stretched
Write-Host "Wallpaper set successfully."
} catch {
Write-Host "Failed to set the wallpaper: $_"
exit 1
}
# Refresh the desktop to apply the changes
try {
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
public class Wallpaper {
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
}
'@
# Correct method call with proper closing parenthesis
[Wallpaper]::SystemParametersInfo(20, 0, $WallpaperPath, 0x01 -bor 0x02)
Write-Host "Wallpaper refresh applied successfully."
} catch {
Write-Host "Failed to refresh the wallpaper: $_"
exit 1
}
# Confirm completion
Write-Host "Wallpaper change process completed."