windowspowershellenvironment-variables

How to create System Environment Variables via Powershell?


I wanted to create an Environment Variable that's stored system-wide.

What I've used in Powershell:

$Env:MyVar = 'Some value'

With gci Env: I can confirm, that the variable has been created successfully. However it was still missing in the Advanced System Settings in Windows (Win11).

How do I set it up to be visible in that menu?


Solution

  • I found the solution by the time of writing the question. Though simple I still wanted to share this information.

    To create a system-wide Environment Variable I used the following code:

    [Environment]::SetEnvironmentVariable('MyVariable', 'Some value', 'Machine')
    

    Make sure to start Powershell with elevated privileges (admin) for creating a system-wide variable.