how to code batch file to change wallpaper in Windows 10 and refresh right now? RRS feed

  • Question

  • I have written a simple bat script, but it occasionally take effect, most of the time doesn't. Who can help me to find out how to solve, thanks.

    i want to use bat script refresh wallpaper right now, neither logoff nor reboot.


    @echo off
    ::Set the variable.
    set ImageFile="c:\wallpaper.jpg"
    set WallpaperFile="c:\windows\WEB\Wallpaper\Windows\img0.jpg"
    set CachedFileFolder="%APPDATA%\Microsoft\Windows\Themes\CachedFiles\"
    set TranscodedFile="%APPDATA%\Microsoft\Windows\Themes\TranscodedWallpaper"

    ::Get file permissions.
    takeown /f %WallpaperFile%
    ::takeown /f C:\Windows\Web\4K\Wallpaper\Windows\*.*
    icacls %WallpaperFile% /Grant %USERNAME%:(F)
    ::icacls C:\Windows\Web\4K\Wallpaper\Windows\*.* /Grant %USERNAME%:(F)

    ::Delete old file and copy the new one.
    del %WallpaperFile%
    copy %ImageFile% %WallpaperFile%
    copy %ImageFile% %TranscodedFile%

    ::Specifies where pictures are cached.(i have specified the path: c:\windows\WEB\Wallpaper\Windows\img0.jpg)
    REG ADD "HKCU\Control Panel\Desktop" /V TranscodedImageCache /T REG_BINARY /D 7ac301005e8e0300e0070000400500007221a48ea366d20143003a005c00570069006e0064006f00770073005c005700650062005c00570061006c006c00700061007000650072005c00570069006e0064006f00770073005c0069006d00670030002e006a007000670000 /F

    ::Sets the background image path and presentation mode.
    REG ADD "HKCU\Control Panel\Desktop" /V Wallpaper /T REG_SZ /D %WallpaperFile% /F
    REG ADD "HKCU\Control Panel\Desktop" /V WallpaperStyle /T REG_SZ /D 2 /F
    REG ADD "HKCU\Control Panel\Desktop" /V TileWallpaper /T REG_SZ /D 0 /F

    ::Refresh the background.
    %SystemRoot%\System32\RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True

    =========the end===========


    Monday, January 9, 2017 11:43 AM

All replies

  • This looks like it's mixing things that need to be run with elevated privileges (takeown / icacls), with things that need to be run as the user (HKCU, %AppData%). I'm guessing, depending on how this script is being run, that you're getting one or the other but not both.

    i.e. you either don't have the rights to takeown and copy the file, or you're making changes to the hkcu and/or appdata for the local administrator or service account or whatever.

    These should either be separate scripts run in ways appropriate for what they're doing, or everything should be doable by the logged on user whether they have admin privileges or not. We take the latter approach - our login script creates the bitmap at the appropriate resolution inside the user's profile, sets the reg keys, and invokes the DLL call to redraw. No icacls, takeown, or changes to things inside c:\windows needed, for the user wallpaper at least.

    Monday, January 9, 2017 12:52 PM