How to Clear Windows Desktop Wallpaper Before BGInfo Applies New Background via Script on Logon


2 views

When deploying BGInfo through the all-users startup folder in Windows environments, we often encounter display issues when users connect via RDP with varying screen resolutions. The problem manifests as "ghosted" wallpaper artifacts because BGInfo renders its output at slightly different positions each time, creating visual corruption over multiple logons.

The Windows wallpaper subsystem maintains a cached copy of previous backgrounds. When BGInfo generates a new bitmap:

1. It creates a new desktop wallpaper bitmap
2. The system composites this with existing wallpaper data
3. Different screen resolutions cause positional offsets
4. No native BGInfo setting clears previous state

Create a pre-BGInfo execution script (ResetWallpaper.ps1):

# Force clear wallpaper cache before BGInfo runs
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);
}
'@

# Set empty wallpaper first
[Wallpaper]::SystemParametersInfo(0x0014, 0, "", 0x01)

# Optional: Set solid color as interim wallpaper
[Wallpaper]::SystemParametersInfo(0x0014, 0, "C:\\Windows\\Web\\Wallpaper\\SolidColors\\black.jpg", 0x01)

Modify your startup sequence in the all-users startup folder:

  1. ResetWallpaper.bat (runs first):
    @echo off
    powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\ResetWallpaper.ps1"
  2. BGInfo.bat (runs after with 30 sec delay):
    @echo off
    timeout /t 30
    start "" "C:\Tools\BGInfo\bginfo.exe" "C:\Tools\BGInfo\config.bgi" /timer:0 /silent

For environments restricting PowerShell, use this REG file:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Control Panel\Desktop]
"Wallpaper"=""
"WallpaperStyle"="0"
"TileWallpaper"="0"

Execute via reg import before BGInfo runs.

Check successful reset with:

reg query "HKCU\Control Panel\Desktop" /v Wallpaper

The value should show empty string ("") before BGInfo execution.


When using BGInfo in environments with shared accounts and varying RDP resolutions, the wallpaper accumulates artifacts because each login session renders the BGInfo output at slightly different positions. Here's why this happens:

  • BGInfo writes the wallpaper image to the same file location (typically %APPDATA%\Microsoft\Windows\Themes\CachedFiles)
  • Windows caches the wallpaper between sessions
  • Different screen resolutions cause minor positioning shifts in text placement

Create a batch script that runs before BGInfo to clear the wallpaper cache:

@echo off
:: Clear wallpaper cache via registry
reg delete "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /f
reg delete "HKEY_CURRENT_USER\Control Panel\Desktop" /v WallpaperStyle /f
reg delete "HKEY_CURRENT_USER\Control Panel\Desktop" /v WallpaperOriginX /f
reg delete "HKEY_CURRENT_USER\Control Panel\Desktop" /v WallpaperOriginY /f

:: Force refresh
rundll32.exe user32.dll, UpdatePerUserSystemParameters

For more control, use this PowerShell script (save as ResetWallpaper.ps1):

# Remove any existing wallpaper
Remove-Item -Path "$env:APPDATA\Microsoft\Windows\Themes\*.*" -Force -ErrorAction SilentlyContinue

# Reset registry values
$regPath = "HKCU:\Control Panel\Desktop"
Remove-ItemProperty -Path $regPath -Name "Wallpaper" -ErrorAction SilentlyContinue
Remove-ItemProperty -Path $regPath -Name "WallpaperStyle" -ErrorAction SilentlyContinue

# Trigger refresh
Add-Type @"
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);
}
"@
[Wallpaper]::SystemParametersInfo(0x0014, 0, $null, 0x01)

For automated deployment via Group Policy, combine these approaches:

  1. Create a scheduled task that runs at user logon
  2. Sequence the actions:
    • First run the wallpaper reset script
    • Then launch BGInfo with appropriate parameters
  3. Use BGInfo's /timer:0 parameter to ensure immediate execution

Example BGInfo command line in your logon script:

start /wait powershell.exe -ExecutionPolicy Bypass -File "\\server\share\ResetWallpaper.ps1"
start /wait bginfo.exe "\\server\share\config.bgi" /timer:0 /silent /nolicprompt

If modifying BGInfo's config is preferred, try these settings in your .bgi file:

  • Under Position, set Background Mode to "Tile"
  • Enable Replace existing wallpaper option
  • Set Wallpaper Style to "Fill" for consistent appearance