Set Environment Variables With PowerShell

This blog post will show you how to set up persistent environment variables with PowerShell on a Windows machine.

PowerShell 7.3 allows us to set persistent environment variables on a user or machine scope. These variables will remain available to applications and users after restarting the machine.

Once an environment variable is set, you can view it with all the other environment variables if you go to.

Open System Control Panel -> System -> Advanced System Settings
Click on the Advanced tab
Select Environment Variable
When you open the environment variables, you see two scopes: User and Machine.

Add Environment Variable to User Scope

In the example below, I will add an environment variable called AZURE_OPENAI_ENDPOINT to the user scope.

[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'https://australiaeast.api.cognitive.microsoft.com/', 'User')

Add Environment Variable to Machine Scope

I will use the following cmdlet to add the same environment variable to the machine scope.

[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', 'https://australiaeast.api.cognitive.microsoft.com/', 'Machine')

Remove

To remove an environment variable, I will use the following cmdlet to set the variable to null.

[System.Environment]::SetEnvironmentVariable('AZURE_OPENAI_ENDPOINT', , 'Machine')

List

To list all the environment variables on your system and check that the variable was added successfully, run the following cmdlet.

Get-ChildItem Env:


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.