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
)

Leave a comment