I am attempting to modify the System Path Environment Variable by doing the following. Unfortunately, what I'm observing is that if I hardcode the string it works, but I use a variable (which is what I would prefer to do), it doesn't work.
I get no errors; it just doesn't work. Here's my code:
$GlobalEnvPath = "C:\Path\ToApp\"
$CurrentEnvPath = [System.Environment]::GetEnvironmentVariable('PATH','Machine')
If ($CurrentEnvPath –match ".+?\;$") { $CurrentEnvPath = $CurrentEnvPath –replace ".{1}$" } #Sanitize the acquired string to remove any trailing semi-colons
$TempNewEnvPath = $CurrentEnvPath.Replace("$GlobalEnvPath",$null) #Find and replace the installation directory with a null value
$NewEnvPath = $TempNewEnvPath.Replace(";;",$null) #Find and replace any double semi-colons that may be present
[System.Environment]::SetEnvironmentVariable('PATH',$NewEnvPath,'Machine') #Finally, let’s write our changes back to the system registry
The part I'm having issues with is:
$TempNewEnvPath = $CurrentEnvPath.Replace("$GlobalEnvPath",$null)
Any help would be very much appreciated, thanks.