Remove environmental variable

Lets round off looking at environmental variables by looking at how to remove them

001
002
003
004
005
006
007
008
009
010
011
012
013
014

function remove-environment {
param (
 [string][ValidateNotNullOrEmpty()]$name,
 [switch]$perm,
 [switch]$machine
)
$value = ""
if (-not $perm) {Remove-Item -Path env:\$($name) -Force}
else {
 if ($machine){$type = "Machine"}
 else {$type = "User"}
 [System.Environment]::SetEnvironmentVariable($name, $value, $type)
}
}

 

If you want to remove it permanently use the –perm switch otherwise it is just removed from the session

Leave a Reply