PowerShell Set Environment Variables

PowerShell Set Environment Variables

Environment variables store data that is used by the operating system and other programs. It is a value available to all processes running in a given system and can be specific to a user, machine or process. Creating, editing or deleting environment variables via PowerShell is a lot quicker than using GUI.

Scope of Environmental Variables in Windows

There are 3 different scopes for environment variables

  • User Scope Environment Variables – These are linked to the user who is currently executing the process.
  • System Scope Environment Variables – These are linked to the system and apply to all users who log in to this computer.
  • Process Scope Environment Variables – These are created dynamically by Windows and linked to user or system environment variables.

View current environment variables using PowerShell

Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the operating system we can use the below command

#View current environment variables
Get-ChildItem -Path Env:

#To get specific environment variable, type variable name after env:
Get-ChildItem -Path Env:\SystemDrive
#

How to set environment variables using PowerShell

We can create new environment variables, and view, edit or copy environment variables via PowerShell using item cmdlets or using System. Environment methods

Using Item cmdlets

We can create a new environment variable using New-Item, set the value of an environment variable with Set-Item, list environment variables with Get-Item, copy the environment variable with Copy-Item and delete the environment variable with Remove-Item.

#Create a new environment variable using New-Item
New-Item -Path Env:\MyOrg -value 'mshub.com'

#Set value of an environment variable with Set-Item
Set-Item -Path Env:\MyOrg -value 'mshub'

#List specific environment variables with Get-Item
Get-Item -Path Env:\MyOrg

#Copy the environment variable with Copy-Item
Copy-Item -Path Env:\MyOrg -Destination Env:\MyOrg2 -PassThru

#Delete the environment variable with Remove-Item
Remove-Item -Path Env:\MyOrg* -Verbose
#

Using System.Environment methods

The System.Environment class provides the GetEnvironmentVariable and SetEnvironmentVariable methods to get and modify environment variables. By using System.Environment class, we can set the Environment Variable to be persistent and scoped for user or computer

Set an Environment Variable scoped for the Local Session

The below PowerShell command will set the environment variable scoped for the local PowerShell Console

#Set an Environment Variable scoped for Local PowerShell session
[Environment]::SetEnvironmentVariable('MyOrg','mshub.com')
#

Set an Environment Variable scoped for User

Below PowerShell command will set persistent environment variable scoped for user

#Set an Environment Variable scoped for User
[Environment]::SetEnvironmentVariable('MyOrg','mshub.com', ‘User’)
#

Set an Environment Variable scoped for the Machine

Below PowerShell command will set persistent environment variable scoped for the machine

#Set an Environment Variable scoped for Machine
[Environment]::SetEnvironmentVariable('MyOrg','mshub.com', ‘Machine’)
#

Get an Environment Variable using GetEnvironmentVariable class

The below PowerShell command will get the environment variable using the GetEnvironmentVariable class

#Get environment variable using GetEnvironmentVariable class
[Environment]::GetEnvironmentVariable('MyOrg')
#

Remove an environment variable using GetEnvironmentVariable class

We can remove an environment variable with the SetEnvironmentVariable method by specifying an empty string for the variable’s value.

#Remove an environment variable scoped for user
[Environment]::SetEnvironmentVariable('MyOrg','', ‘User’)

#Remove an environment variable scoped for machine
[Environment]::SetEnvironmentVariable('MyOrg','', ‘Machine’)
#

View and set environment variables using GUI

If you still prefer to use GUI instead of PowerShell to view or set Environment variables follow the below listed steps. You can add or edit existing environment variables in the User and System (Machine) scopes. Windows writes these values to the Registry so that they persist across sessions and system restarts.

  • Open the System Control Panel
  • Select System
  • Select Advanced System Settings
  • Go to the Advanced tab
  • Select Environment Variables
  • Make your changes

Conclusion

In conclusion, setting environment variables using PowerShell is a simple process that can be done by following the steps outlined in this article. By understanding how to set environment variables, you can customise your operating system and programs to fit your needs. We have also outlined how this can be performed using GUI for administrators who prefer GUI instead of PowerShell.

Share your love
Asif Syed
Asif Syed

I am a System Engineer with 15+ years of hands-on experience in Microsoft technology. My expertise lies in creating and optimizing Microsoft-based systems, delivering efficient solutions aligned with business goals.

Leave a Reply

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

Stay informed and not overwhelmed, subscribe now!