Private/Update-EnvironmentVariableSettings.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$Win32 = Add-Type -Namespace "EnvVar.Import" -Name "Win32" -PassThru -MemberDefinition @"
            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            public static extern IntPtr SendMessageTimeout(
                IntPtr hWnd,
                uint Msg,
                UIntPtr wParam,
                string lParam,
                uint fuFlags,
                uint uTimeout,
                out UIntPtr lpdwResult
            );
 
            [DllImport("kernel32.dll")]
            public static extern uint GetLastError();
"@
 -ErrorAction Continue
function Update-EnvironmentVariableSettings {
    param (
    )

    begin {
    }

    process {
        $HWND_BROADCAST = [IntPtr] 0xffff
        $WM_SETTINGCHANGE = 0x1a
        $result = [UIntPtr]::Zero

        $ret = $Win32::SendMessageTimeout(
            $HWND_BROADCAST,
            $WM_SETTINGCHANGE,
            [UIntPtr]::Zero,
            "Environment",
            2,
            5000,
            [ref] $result
        )
        if ($ret -eq 0) {
            $errorcode = $Win32::GetLastError($ret)
            Write-Error "failed to update environment setting (error code: $errorcode)"
        }
    }

    end {
    }
}