Changing environmental variables

This is similar to creating them.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018

function set-environment {
param
 (
 
[string][ValidateNotNullOrEmpty()]$name,
 [string][ValidateNotNullOrEmpty()]$value,
 [switch]$perm,
 [switch]$machine

)

 
if (-not $perm) {Set-Item -Path env:\$($name) -Value $($value
)}
 
else
 {
 
if ($machine){$type = "Machine"
}
 
else {$type = "User"
}
 
[System.Environment]::SetEnvironmentVariable($name, $value, $type
)
 }
 
 
Get-Item -Path env:\$($name)
}

 

We use the same variables as when creating a variable.  If a permanent change is required we use the –perm switch but remember that the change won’t be seen until PowerShell is restarted

Leave a Reply