Set the PATH Environment Variables in Windows PowerShell

Marion Paul Kenneth Mendoza Dec 21, 2022 Dec 15, 2021
  1. Use $Env:PATH to Set the PATH Environment Variables in Windows PowerShell
  2. Using the [Environment] Method to Set the PATH Environment Variables in Windows PowerShell
Set the PATH Environment Variables in Windows PowerShell

The PATH variable is a system environment variable that your operating system uses to locate executables from the command line interface. We usually use this when it comes to developing various programs with different types of programming languages. However, setting this up inside the PowerShell environment is quite different.

Use $Env:PATH to Set the PATH Environment Variables in Windows PowerShell

Usually, we can set the PATH variable by navigating through the control panel of our operating system. However, inside Windows PowerShell, we can output all our file paths using the $Env:PATH environment variable.

Example Code:

Write-Output $Env:PATH


C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files\Common Files\Microsoft Shared\Microsoft Online Services;C:\Windows\System32\WindowsPowerShell\v1.0\

We will also use $Env:PATH to set our PATH environment variables in PowerShell.

Before starting, save a copy of your existing PATH variable by executing the snippet below.

$Env:PATH >> Env_Path.txt

Other software also relies on the PATH environment variable, and accidentally overwriting this may lead to multiple issues. Performing the syntax above will serve as your primary backup for your environment variable values.

To set a new path, you will need to append your new path to the variable by performing a simple string operation.

$Env:PATH += ";C:\Program Files\Scripts"

Don’t forget to add the semicolon (;), which will act as a separator between your file paths, and the plus (+) operator to append the value to the variable.

If you need your new path to be called first in before the pre-determined paths, you can insert it at the beginning using the syntax below.

$Env:PATH = "C:\Program Files\Scripts;$Env:PATH"

Using the $Env:PATH variable will only temporarily change the PATH environment variable in a PowerShell session. Closing the PowerShell window will revert the PATH environment variable to its pre-determined state.

To permanently change the value of our PATH environment variable, we can use the following method below.

Using the [Environment] Method to Set the PATH Environment Variables in Windows PowerShell

An alternative way to change PATH environment variables is by using the [Environment] variable, which will employ the base .NET framework elements.

Remember that this will change the PATH environment variables permanently. It will be persistent across all scripting environments.

To change the PATH environment variable, run the following syntax below.

[Environment]::SetEnvironmentVariable("PATH", $Env:PATH + ";C:\Program Files\Scripts", [EnvironmentVariableTarget]::Machine)

The function [Environment]::SetEnvironmentVariable in the syntax above has three parameters.

  • The type of environment variable that needs to be modified, in this case, is PATH.
  • The new value of the environment variable.
  • And the environment variable target, which specified the location of the environment variable. (Machine, User, or Process level)

Getting the output of the $Env:PATH now using Write-Output will reflect the old PATH environment variable before the change. Close your current PowerShell window and open a new one to update and see the result.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.