14

I'm trying to use a command line implementation to change the PATH environment variable to remove a path, so I don't have to manually remove it on a bunch of machines.

I have found this, which I can't seem to get it to work:

%Path:str1=str2%

str1 is the path and str2 is null, which I'm not sure how to set it to null on the command line.

If there is another way, I would be glad to give it a try.

daaawx
  • 3,273
  • 2
  • 17
  • 16
Bruce227
  • 893
  • 5
  • 14
  • 25

5 Answers5

32

I have found this, which I can't seem to get it to work: %Path:str1=str2% str1 is the path and str2 is null, which I'm not sure how to set it to null on the command line.

Not sure why this didn't work for you, but here is an example that does work (at least on Windows XP).

set path=%path:c:\windows\system32;=%

This will remove "c:\windows\system32;" from the path variable. Make sure you have the ; on the end otherwise it may partially remove some other paths.

Remember that this will only affect the current instance of the command prompt. If you quit or work in a different command prompt, any changes you made to the environment variables will be lost.

Grant Peters
  • 7,691
  • 3
  • 45
  • 57
  • 1
    In some environments, Windows Command Extensions are disabled so you will need to enable Command Extensions + Delayed Expansion for the substitution to work: `setlocal ENABLEEXTENSIONS && setlocal ENABLEDELAYEDEXPANSION`. – mvanle Dec 29 '13 at 03:15
  • Worked on my Win 10 thks mate – Arnaud Bouchot Sep 26 '17 at 13:08
4

Using VBScript, you can get the path variable:

dim shell, env, path, path_entries
set shell = createobject("wscript.shell")
set env = shell.environment("system")
path = env("path")

Then split to get an array of the pieces:

path_entries = split(path, ";")

Set any entries to an empty string to remove them:

path_entries(3) = ""

Then reconstruct the path:

path = join(path_entries, ";") ' elements in path are delimited by ";"
env("path") = path
Community
  • 1
  • 1
Bryan Ash
  • 4,385
  • 3
  • 41
  • 57
2

There's a difference between changing Path variable for a current process and/or for child processes, to changing the default load state of the variable when windows starts.

You might probably be able to do it with WMI. If not, take procmon and see what "My Computer" is doing when you edit a system variable. This will enable you to write a script.

Pavel Radzivilovsky
  • 18,794
  • 5
  • 57
  • 67
0

In a vbScript command file (.cmd) or (.bat), you can use the following to remove an environment variable:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Environment("Process").Remove("PATH")
Ken Richards
  • 2,937
  • 2
  • 20
  • 22
  • I'm not looking at trying to remove the environmental variable. I'm trying to change the list of paths in the environmental variable and remove one that is no longer needed. – Bruce227 Jun 18 '10 at 19:41
0

There is an easier way instead of using the command prompt. Right click on "My Computer" go to advanced system settings, at bottom click Environment variables, highlight "PAth" and click "Edit". You can add, delete or change the order of directories in your path there.

Hope this helps somebody, 2

2ninup
  • 1