Sergey Nivens - Fotolia

Q
Manage Learn to apply best practices and optimize your operations.

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:

$env:Path

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 
"^$regexAddPath\\?"}

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 (
        [string]$addPath
    )
    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
Files\Oracle\Java\javapath'

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 (
        [string]$AddPath,
        [string]$RemovePath
    )
    $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.

Dig Deeper on Scripting, Scheduling and IT Orchestration

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

How does your organization use PowerShell automation?
Cancel

-ADS BY GOOGLE

SearchDataCenter

SearchAWS

SearchServerVirtualization

TheServerSide.com

SearchCloudComputing

Close