Introduction To PowerShell Environment Variables

Learn how to use PowerShell to manage your system's environment variables.

Brien Posey

September 30, 2024

4 Min Read
technology interface with a person's hand drawing gears and cogs

Environment variables are system-defined variables that store values used by the operating system and applications. For example, on a Windows machine, information like the CPU type and the number of processors are stored as environment variables.

Understanding the types of environment variables

In a Windows environment, there are three main types of environment variables:

  1. System Environment Variables: These apply to the entire operating system. They store critical information like the system path or the location of the system root directory.

  2. User Environment Variable: Similar to system environment variables, these store the values specific to the user, such as the location of the user’s home directory.

  3. Process Environment Variable: These variables are created dynamically when you run a process, and they are typically not visible outside the process. They are temporary and usually removed when the process terminates.

Managing environment variables in PowerShell

You can manage environment variables in Windows through PowerShell, the Windows GUI, or the Command Prompt. PowerShell is preferred because it allows for consistent management across multiple systems. For example, you can use a PowerShell script to ensure that all environment variables on your PC are configured consistently.

Related:How To Use Dynamic Parameters in PowerShell Functions

Setting up Your Environment Variables With PowerShell

When managing environment variables in PowerShell, you must decide whether the changes to variable assignments should be temporary or permanent. The choice determines which command to use.

Using Set-Item for temporary adjustments

To temporarily modify an environment variable’s value, use the Set-Item cmdlet. The cmdlet requires two parameters:

  • Path: Specifies the environment variable’s name.

  • Value: Assigns the new value to the variable.

For example, to set the Temp environment variable to C:\Temp, you would run:

Set-Item -Path Env:Temp -Value “C:\Temp”

As a shortcut, you can directly assign a value to an environment variable without using Set-Item:

$env:Temp=”C:\Temp”

Making changes stick with SetEnvironmentVariable

To make an environment variable assignment permanent, use the SetEnvironmentVariable method from the .NET Framework instead of a PowerShell cmdlet.

For example, to permanently set the Temp environment variable to C:\Temp for a user named Brien, I would use this command:

[System.Environment]::SetEnvironmentVariable("Temp", "C:\Temp", "Brien")

The process works roughly the same for system-level changes, except you specify a machine name instead of a username.

Advanced Usage of PowerShell Environment Variables

As you can see, modifying an environment variable in PowerShell is relatively straightforward. However, there is at least one slightly more advanced technique you should know about.

Related:Building Graphical PowerShell Tools: A Three-Part Guide

Appending vs. overwriting: Additive changes to environment variables

Occasionally, you may need to add a value to an existing environment variable instead of completely overwriting it. The Path environment variable is perhaps the best example. If you used a technique described above to add a new location to the Path environment variable, the modification would overwrite the current contents, which can cause system issues. You can avoid this by appending the new value to the existing content.                                          

To temporarily append a location to the Path environment variable, use the += variable assignment method. For example, to add C:\Temp to the existing path, you would use this command:

$Env:Path +=”;C:\Temp”

Note the semicolon inside the quotation marks—it separates the new location from the existing ones in Path.

If you want to make this change permanent, the command will look like this:

[System.Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Temp", [System.EnvironmentVariableTarget]::User)

The command instructs .NET to add the new location to the existing path environment variable.

Removing and Modifying Environment Variables

You may sometimes need to remove or edit an environment variable in PowerShell. Fortunately, this is easy to do. Editing an environment variable is no different than assigning it a new value.

Related:How To Find a File’s Path in PowerShell

Safely removing an environment variable

The technique for removing an environment variable depends on whether the variable is temporary or permanent.

Temporary Variables: To remove a variable that only exists during your session, use the Remove-Item cmdlet. For example, to remove the Temp environment variable, use the following command:

Remove-Item -Path Env:Temp

Permanent Variables: It’s best to avoid removing permanent environment variables if you can. However, if you need to do so, you can use this command:

[System.Environment]::SetEnvironmentVariable("Temp", $null, [System.EnvironmentVariableTarget]::User)

The example removes the Temp environment variable for the user, but you can modify it to remove other variables by changing its name in the command.

Editing existing variables for cleaner management

To modify an existing environment variable, replace its current value with a new one. You can use the Set-Item cmdlet and the [System.Environment]::SetEnvironmentVariable command discussed earlier. Both methods will replace a variable’s existing value.

FAQ: Using PowerShell To Manage Environment Variables

Q: Can changes to environment variables be made permanent?

A: To permanently change an environment variable, you must modify the Windows registry.

  • User-specific environment variables are stored in HKEY_CURRENT_USER\Environment.

  • System-wide environment variables are located in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.

To make permanent changes using PowerShell, you can use the [System.Environment]::SetEnvironmentVariable method.

Q: How do I view specific environment variables in PowerShell?

A: To view a specific environment variable in PowerShell, you can reference the $env variable followed by the name of the environment variable you wish to display. For example, to see the contents of the Path environment variable, you would type $env:path.

Q: What is the difference between adding and setting an environment variable?

When you add an environment variable, you create a brand-new environment variable that will coexist with your current ones. In contrast, when you set an environment variable, you are not generating a new variable. Instead, you are changing the value associated with an existing environment variable.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

https://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like