title: Windows batch command to make changes effective immediately
body: <p>I have a batch script that lets users change their background from black to white or vice versa.  The problem I'm having is that the script only makes immediate change sometimes, and other times the user has to log off and log back on for the background to change.  Here is what I have so far:</p>
  
  <pre><code>@echo off
  
  call :quiet
  exit /b
  
  :quiet
      :: For comparison, using the black wallpaper registry value
      set "black=C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Themes\MDCBackground_black.bmp"
  
      :: Set reg query result to current
      FOR /F "tokens=2* delims=    " %%A IN ('REG QUERY "HKCU\Control Panel\Desktop" /v Wallpaper') DO SET current=%%B
      :: For debugging purpose.
      ECHO current=%current%
      pause
  
      if "%current%"=="%black%" (
          call :MakeDayWallpaper&gt;nul 2&gt;&amp;1
          :: Make changes without requiring logoff
          RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
      ) else (
          call :MakeNightWallpaper&gt;nul 2&gt;&amp;1
          :: Make changes without requiring logoff
          RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
      )
  EXIT /b
  
  
  :MakeDayWallpaper
      REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "" /f
      REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Themes\MDCBackground_white.bmp" /f
      REG DELETE "hkcu\Software\Microsoft\Internet Explorer\Desktop\General" /v Wallpaper /f
      REG ADD "hkcu\control panel\desktop" /v WallpaperStyle /t REG_SZ /d 2 /f
  EXIT /b
  
  
  :MakeNightWallpaper
      REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "" /f
      REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Themes\MDCBackground_black.bmp" /f
      REG DELETE "hkcu\Software\Microsoft\Internet Explorer\Desktop\General" /v Wallpaper /f
      REG ADD "hkcu\control panel\desktop" /v WallpaperStyle /t REG_SZ /d 2 /f
  EXIT /b
  </code></pre>
  
  <p>The line <code>RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters</code> is the command that allows the immediate change.  When I look at shell I can see that the registry value is changing every time the script is executed, but despite this fact, sometimes the background does not change until the user logs off and logs on.</p>