I am running a powershell script to change the background to a certain set of colors. I would like to do this without rebooting, but unfortunately cannot get the changes to take effect immediately on a windows 7/8 platform. I've found many solutions online but I cannot find one that works for me. I think it may have something to do with setting the SystemParametersInfo, but I don't know for sure. I've seen a few solutions and tried them out for myself, but I can't get them to work either. The registry keys update just find but the changes don't take effect until after I reboot. Below is what I have so far, if anyone seeing anything I could do different I would appreciate the help!
backgroundtest.ps1
Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Background
{
public class Setter {
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);
public const int UpdateIniFile = 0x01;
public const int SendWinIniChange = 0x02;
public const int SetDesktopBackground = 20; <# following examples online to set parameters #>
public static void SetBackground() {
SystemParametersInfo(SetDesktopBackground, 0, "", UpdateIniFile | SendWinIniChange);
RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
key.SetValue(@"WallPaper", 0); <#remove wallpaper#>
RegistryKey key2 = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
key2.SetValue(@"Background", "0 118 163"); <#set background to new color>
key.Close();
key2.Close();
}
}
}
"@
[Background.Setter]::SetBackground()
SystemParametersInfo
call to after you update the registry keys rather than before, as that might cause the system to update its internal state.SystemParametersInfo
to clear out any wallpaper image if you want to end up with a solid color.