Link to home
Create AccountLog in
Avatar of String :-)
String :-)Flag for Australia

asked on

Powershell - How to edit $env:Path using Powershell

Hi Experts,
I am seeking Powershell help on how to remove a single entry from $env:Path
As an example, how would I remove "C:\Program Files (x86)\PuTTY\;" from the following:


PS C:\Users\xxxx> $env:Path

C:\Program Files (x86)\Windows Resource Kits\Tools\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\OpenVPN\bin;C:\Progra

m Files (x86)\PuTTY\;C:\Program Files\Git\cmd;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Users\xxxx\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\Nmap;

Many thanks for any tips.
String :-)


ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account

The command SETX should be used to have a permanent change of variables as PATH.


Note that the changes will only be available for new execution of CMD.EXE, but not for the current opened environment.



I disagree about "should". setx is a relict from the XP era (when it has been not been part of the OS yet).

But you are correct, changes for the global environment are not applied to the current process.

No, setx.exe is still present (and useful) in Windows 2022.


If it can simplify some scripts, why not using the given tools. 

Whatsoever, using setx requires the other steps (determining if the setting is for the user or machine), and only remove that part in the correct scope. It wouldn't make the task more simple than with the PS script I showed, as setx cannot help you in getting that info, and that is the important part.

Avatar of String :-)

ASKER

Thanks to both for your contribution.  Script from Qlemo worked perfectly.. thanks so much for your assistance. Have a great weekend...