PowerShell – delete path value in the Path environment variable

Posted: April 18, 2019 in Scripts

In User environmental path variable we want to delete only specific values (var1 and var2 in this case)

Capture

 

 

 $path = [System.Environment]::GetEnvironmentVariable(
    'PATH',
    'User' # For user variable
    #'Machine' for system variable
)

$path = ($path.Split(';') | Where-Object { $_ -ne 'var1' }) -join ';'

$path = ($path.Split(';') | Where-Object { $_ -ne 'var2' }) -join ';'
# Set it
[System.Environment]::SetEnvironmentVariable(
    'PATH',
    $path,
    'User' # For user variable
    #'Machine' for system variable
)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s