Change Desktop Background color Remotely

oldrich Bukac 21 Reputation points

Hi , I will change Desktop background color on remote computer , I have script , to change color immediately on local machine , when aj run this script Remotely No effect, can help?

$ComputerName = "PC170774"  
Invoke-Command -ComputerName $ComputerName -ScriptBlock {   
$code = @'   
 using System;   
 using System.Drawing;   
 using System.Runtime.InteropServices;   
 using Microsoft.Win32;   
 namespace CurrentUser   
     public class Desktop   
         [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]   
         private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);   
         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]   
         private static extern int SetSysColors(int cElements, int[] lpaElements, int[] lpRgbValues);   
         public const int UpdateIniFile = 0x01;   
         public const int SendWinIniChange = 0x02;   
         public const int SetDesktopBackground = 0x0014;   
         public const int COLOR_DESKTOP = 1;   
         public int[] first = {COLOR_DESKTOP};   
         public static void RemoveWallPaper()   
             SystemParametersInfo( SetDesktopBackground, 0, "", SendWinIniChange | UpdateIniFile );   
             RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);   
             regkey.SetValue(@"WallPaper", 0);   
         public static void SetBackground(byte r, byte g, byte b)   
             int[] elements = {COLOR_DESKTOP};   
             System.Drawing.Color color = System.Drawing.Color.FromArgb(r,g,b);   
             int[] colors = { System.Drawing.ColorTranslator.ToWin32(color) };   
             SetSysColors(elements.Length, elements, colors);   
             RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);   
             key.SetValue(@"Background", string.Format("{0} {1} {2}", color.R, color.G, color.B));   
    Add-Type -TypeDefinition $code -ReferencedAssemblies System.Drawing.dll   
    # An error is thrown if the type [CurrentUser.Desktop] is already created  
    # so we ignore it.  
    [CurrentUser.Desktop]::SetBackground($R, $G, $B)  

a try copy this script in 1.ps1 file to remote computer and run :

$RemoteScriptPath = "C:\ps1"  
Invoke-Command -ComputerName $Computername -ScriptBlock { Start-Process powershell.exe -ArgumentList $using:RemoteScriptPath -Wait }  


with No effect,,,

Can change Background color on remote machine to see effect immediately(no need logoff or restart)?

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,583 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,356 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 95,496 Reputation points MVP

    Could it be the remote script is running interactive without a "current user context/profile"?

    This would explain, why the script is working if started locally in a user context and not working if invoked remote.

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Andreas Baumgarten

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,486 Reputation points Microsoft Vendor


    Have you tried to login to the remote server and run the script? Please also see if this works.

    Invoke-Command -FilePath $ScriptPath  -ComputerName $Computername  

    Best Regards,


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. oldrich Bukac 21 Reputation points

    Yes I try it, no effect ,

    when run script localy works fine, BG change immediately.

    0 comments No comments

  3. Sebastien Vinchon 1 Reputation point

    Did you find a solution?
    I have the same issue (almost).
    I am using a java agent to trigger the powershell script call remotely.
    I does not fail but does not produce the expected result either.
    The same exact script work perfectly when ran from within the target machine manually.

    0 comments No comments