# 1. Define the image URL and save location $url = "https://cdn.xorydev.xyz/currently_selected_assets/wallpaper.png" $wallpaperPath = "C:\Users\Public\Pictures\wallpaper.png" # Ensure the destination directory exists $wallpaperDir = Split-Path -Path $wallpaperPath -Parent if (-not (Test-Path -Path $wallpaperDir)) { New-Item -ItemType Directory -Path $wallpaperDir -Force } # 2. Download the image try { Invoke-WebRequest -Uri $url -OutFile $wallpaperPath -UseBasicParsing -ErrorAction Stop } catch { Write-Error "Failed to download the wallpaper. Please check the URL and your internet connection." # Exit the script if the download fails return } # 3. Update registry to point to the new wallpaper Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name Wallpaper -Value $wallpaperPath # 4. Define the C# code to call the native Windows API $CSharpCode = @" using System.Runtime.InteropServices; public class WallpaperChanger { // Import the SystemParametersInfo function from user32.dll [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); } "@ # 5. Compile the C# code in memory Add-Type -TypeDefinition $CSharpCode # 6. Define constants needed for the API call $SPI_SETDESKWALLPAPER = 0x0014 $SPIF_UPDATEINIFILE = 0x01 $SPIF_SENDCHANGE = 0x02 # 7. Call the compiled function to apply the wallpaper change immediately [WallpaperChanger]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $wallpaperPath, ($SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE))