Sergey Nivens - Fotolia

Manage the Windows PATH environment variable with PowerShell

Among many other tasks, IT teams can use PowerShell to manage and edit the PATH environment variable. Walk through these basic steps to access, add and remove system paths.

The Windows PATH environment variable is where applications look for executables -- meaning it can make or break a system or utility installation. Admins can use PowerShell to manage the PATH variable -- a process that entails string manipulation.

To access the PATH variable, use:


Depending on your system, the returned PATH variable string could be shorter or longer than the one in Figure 1. A semicolon separates each path in the list.

Returned string for PATH variable
Figure 1: PATH variable string

Splitting the string required the following command:

$env:Path -split ';'

This command in Figure 2 will return a string array of the path values that are a part of the PATH variable.

string array of path values
Figure 2: Path values as seen in a string array

Add to the Windows PATH environment variable

To add to the PATH, append a semicolon and a new path on the end of the long path string. We can use PowerShell to check whether the path we want to add is already in the existing path.

First, pick a path to add:

$addPath = 'C:\TopSecret\Bin'

Path strings that refer to a directory are technically correct with or without a trailing slash -- '\' -- and, either way, that path will resolve correctly. To add a path to the PATH variable, first check whether the path is already there. To do so, check for the existence of the path both with and without the trailing slash.

Iterate through all the existing paths and use the following command to check if the new path is already included with or without a '\' on the end:

$regexAddPath = [regex]::Escape($addPath)
$arrPath = $env:Path -split ';' | Where-Object {$_ -notMatch 

Since we are using regular expressions to check for a trailing '\', escape the $addPath, as it will likely have slashes in it.

After we have our array of paths that doesn't include our new one, we can add our path to it, join them with the semicolon and assign that to the PATH variable:

$env:Path = ($arrPath + $addPath) -join ';'

Then, collect that array together as a single function:

Function Add-PathVariable {
    param (
    if (Test-Path $addPath){
        $regexAddPath = [regex]::Escape($addPath)
        $arrPath = $env:Path -split ';' | Where-Object {$_ -notMatch 
"^$regexAddPath\\?"} $env:Path = ($arrPath + $addPath) -join ';' } else { Throw "'$addPath' is not a valid path." } }

How to remove a path

It's just as simple to remove a path from the Windows PATH environment variable as it is to add one. For this example, circle back to Figure 2, which shows some of the paths on the computer, and remove Java:

$removePath = 'C:\Program Files (x86)\Common

Again, split the PATH and only select the paths that don't match the $removePath with a possible trailing slash. This time, however, we will simply assign that output back to the PATH variable:

$regexRemovePath = [regex]::Escape($removePath)
$arrPath = $env:Path -split ';' | Where-Object {$_ -notMatch 
"^$regexRemovePath\\?"} $env:Path = $arrPath -join ';'

Because this process is so similar to adding a path, we can combine these processes into one function rather than have two:

Set-PathVariable {
    param (
    $regexPaths = @()
    if ($PSBoundParameters.Keys -contains 'AddPath'){
        $regexPaths += [regex]::Escape($AddPath)

    if ($PSBoundParameters.Keys -contains 'RemovePath'){
        $regexPaths += [regex]::Escape($RemovePath)
    $arrPath = $env:Path -split ';'
    foreach ($path in $regexPaths) {
        $arrPath = $arrPath | Where-Object {$_ -notMatch "^$path\\?"}
    $env:Path = ($arrPath + $addPath) -join ';'

Editor's note: This expert answer is the final part in a three-part series on PowerShell automation. See another expert answer on ACL folder management, and one on Windows event log monitoring.

Next Steps

PowerShell looping examples for the 4 key commands

Connect data with PowerShell's Join-Object module

How to secure passwords with PowerShell

Dig Deeper on Systems automation and orchestration