0

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.

4
  • 1
    ..are you editing your code in MS Word or something? You have smart quotes all over your snippet. Commented Apr 11, 2019 at 16:26
  • Lol, no. I'm coding in the ISE but it's on an airgapped network so I had to retype in a text file. I will edit accordingly. Commented Apr 11, 2019 at 17:15
  • It's not clear what actual issue you are having. What is the result you are getting and what are you expecting? Commented Apr 11, 2019 at 17:22
  • No need to worry anymore, I have resolved the problem. Turns out I needed to wrap my variable inside a "$()" as such: "$($GlobalEnvPath)" Commented Apr 11, 2019 at 17:38

2 Answers 2

1

Here's my suggested solution:

$path = @([Environment]::GetEnvironmentVariable('PATH', 'Machine') -split ';')

$exclude = [regex]::Escape('C:\Path\ToApp\' -replace '\\$')
$newPath = $path -notmatch $exclude -join ';'

[Environment]::SetEnvironmentVariable('PATH', $newPath, 'Machine')

This removes a lot of room for error. String#Replace is case-sensitive, for example. I also leave the trailing slash absent for that possible edge case.

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, I like your approach as well. Thanks for sharing.
0

Turns out I needed to wrap my variable inside a $() like so:

$TempNewEnvPath = $CurrentEnvPath.Replace("$($GlobalEnvPath)",$null)

I'm not sure exactly why this is but another colleague of mine brought this to my attention and I thought I'd share it in case someone else ran into this problem. If anyone out there can shed some light on why this is please feel free to do so for our common edification.

If I find additional information as well, I will post back to this answer.

1 Comment

That's actually entire unnecessary. "$var" is equivalent to "$($var)"..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.