none
PowerShell: Change desktop wallpaper on remote computer

    Question

  • Hey,

    I was trying to have some fun with my boyfriend, chaning his wallpaper. I couldn't figure it out, so I asked him for help, but he couldn't figure it out either. We both run win7, he's running PowerShell v2 and I got v3. We added each-others computers to trusted hosts, set execution policy to unrestricted, enabled psremoting, configured a home group, and know each-others credentials (admin). This is what we have tried:

    Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    namespace Wallpaper
    {
       public enum Style : int
       {
           Tile, Center, Stretch, NoChange
       }
       public class Setter {
          public const int SetDesktopWallpaper = 20;
          public const int UpdateIniFile = 0x01;
          public const int SendWinIniChange = 0x02;
          [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
          private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
          public static void SetWallpaper ( string path, Wallpaper.Style style ) {
             SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
             RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
             switch( style )
             {
                case Style.Stretch :
                   key.SetValue(@"WallpaperStyle", "2") ; 
                   key.SetValue(@"TileWallpaper", "0") ;
                   break;
                case Style.Center :
                   key.SetValue(@"WallpaperStyle", "1") ; 
                   key.SetValue(@"TileWallpaper", "0") ; 
                   break;
                case Style.Tile :
                   key.SetValue(@"WallpaperStyle", "1") ; 
                   key.SetValue(@"TileWallpaper", "1") ;
                   break;
                case Style.NoChange :
                   break;
             }
             key.Close();
          }
       }
    }
    "@
    
    [Wallpaper.Setter]::SetWallpaper( 'C:\temp\Wallpaper.bmp', 2 )

    invoke-command { powershell.exe -noprofile -executionpolicy Bypass C:\temp\ChangeDesktop.ps1 } -computername Lucius-PC -Credential $cred

    Invoke-Command -ComputerName Lucius-Pc { Set-ItemProperty -Path "HKCU:Control Panel\Desktop" -name WallPaper -value c:\temp\wallpaper.bmp } -cred $cred

    Thursday, May 16, 2013 7:47 PM

Answers

  • Hi,

    I think al dunbar pointet in the right directions. Setting the wallpaper for the "current user" is not what you need. You have to go to HKEY_USERS and the the Wallpaper for the right SID. Also the explorer need's a restart as far as i know to show the new wallpaper. Please note that the path for the wallpaper must exists on the target machine. ;-)

    Here an example for the SID:

    cp "image.jpg" \\<IP>\C$
    $RemoteReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', "<IP>")
    $WallPaper= $RemoteReg.OpenSubKey("S-1-5-21-780093305-1579027723-3042286928-500\Control Panel\Desktop",$True)
    $WallPaper.SetValue("Wallpaper","C:\image.jpg")

    Greetings Malte

    Friday, May 17, 2013 8:18 AM

All replies

  • That is what you have tried. In addition to the wallpaper not being set, what else happened, for example, were there any error messages?

    One possible issue could be that the wallpaper setting is specific to the user profile. Where in the above is the profile specified, or is it supposed to be deduced from the credentials under which it runs?


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Thursday, May 16, 2013 9:30 PM
  • Hi,

    I think al dunbar pointet in the right directions. Setting the wallpaper for the "current user" is not what you need. You have to go to HKEY_USERS and the the Wallpaper for the right SID. Also the explorer need's a restart as far as i know to show the new wallpaper. Please note that the path for the wallpaper must exists on the target machine. ;-)

    Here an example for the SID:

    cp "image.jpg" \\<IP>\C$
    $RemoteReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', "<IP>")
    $WallPaper= $RemoteReg.OpenSubKey("S-1-5-21-780093305-1579027723-3042286928-500\Control Panel\Desktop",$True)
    $WallPaper.SetValue("Wallpaper","C:\image.jpg")

    Greetings Malte

    Friday, May 17, 2013 8:18 AM
  • Hi,

    Just checking in to see if the suggestions were helpful. Please let us know if you would like further assistance.

     

    If you have any feedback on our support, please click here .

     


    Cataleya Li
    TechNet Community Support

    Monday, May 20, 2013 6:46 AM
  • We actually got it to work, just needed a restart.............
    Saturday, July 06, 2013 9:01 PM