Skip to content

Instantly share code, notes, and snippets.

@0xfeeddeadbeef
Created May 15, 2023 09:07
Show Gist options
  • Save 0xfeeddeadbeef/2c03971709f457bd726cea20c52f94d7 to your computer and use it in GitHub Desktop.
Save 0xfeeddeadbeef/2c03971709f457bd726cea20c52f94d7 to your computer and use it in GitHub Desktop.
Send WM_SETTINGCHANGE message from a PowerShell script
function _BroadcastSettingsChange
{
if ([Environment]::UserInteractive)
{
$win32native = @'
namespace EnvUtils
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
[SuppressUnmanagedCodeSecurity]
public static class Win32Native
{
public static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
public const int WM_SETTINGCHANGE = 0x001A;
[DllImport("user32.dll", SetLastError = true, BestFitMapping = false)]
[ResourceExposure(ResourceScope.Machine)]
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult);
}
}
'@
if (-not ([System.Management.Automation.PSTypeName]'EnvUtils.Win32Native').Type)
{
Add-Type -Language CSharp -TypeDefinition $win32native
}
[EnvUtils.Win32Native]::SendMessageTimeout([EnvUtils.Win32Native]::HWND_BROADCAST, [EnvUtils.Win32Native]::WM_SETTINGCHANGE, [IntPtr]::Zero, 'Environment', 0, 1000, [IntPtr]::Zero) | Out-Null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment