2

The following code does not return "Y" as expected. Only in the next session (another new window) it works? I would expect it to be available immediately?

    [Environment]::SetEnvironmentVariable("X", "Y", "Machine")
    Write-Host $env:X

2 Answers 2

1

You must do this since the process gets env vars on start, not while running (i.e. you would have to restart shell for this to work your way):

 [Environment]::SetEnvironmentVariable("X", "Y", "Machine")
 $Env:X = "Y"

There is also a way to broadcast this to other windows using WM_SETTINGCHANGE

To effect a change in the environment variables for the system or the user, broadcast this message with lParam set to the string "Environment".)

# Notify system of change via WM_SETTINGCHANGE
    if (! ("Win32.NativeMethods" -as [Type]))
    {
        Add-Type -Namespace Win32 -Name NativeMethods -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);
"@
    }

    $HWND_BROADCAST = [IntPtr] 0xffff; $WM_SETTINGCHANGE = 0x1a; $result = [UIntPtr]::Zero
    [Win32.Nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [UIntPtr]::Zero, "Environment", 2, 5000, [ref] $result) | out-null
}
Sign up to request clarification or add additional context in comments.

Comments

1

As far as I know, a process loads the environment variables only once (at start). But you can change it using:

[Environment]::SetEnvironmentVariable("X", "Y", "Process") # for the current session

Note: You probably want to set both:

[Environment]::SetEnvironmentVariable("X", "Y", "Machine")
[Environment]::SetEnvironmentVariable("X", "Y", "Process")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.