I got it into my head earlier this evening that maybe I couldn’t reach my files that should be registered in the PATH variable because it had been truncated, after all when I ran

I did get a truncated text. However that is probably just due to the output buffer being capped looking at this post about maximum length of environment variables.

By this point however, I had already begun building a script to try and shorten down my PATH variable, I mean, who would ever do that manually?……. I mean, who wouldn’t spend two hours writing a script to do it?……. I mean, yea, you could’ve used a regex and split it, done a 5 second ocular inspection, decided that it couldn’t be improved and left it at that but that wouldn’t be fun would it?……. ok. I got carried away.

So, anyway, I did some research, a lot of trial and error and the script I came up with not only removes any multiples of registered directories and missing ones in the PATH variable but also showcases some cool useful stuff you can do with a VBScript or command prompt, such as:

  • Restarting current command prompt
    • Which is good for reloading environment variables
    • Simply:
  • Running an elevated command from within the script
  • Grabbing output from another script
  • Changing environment variables from within a script
  • Checking whether or not you’re running in elevated mode

So the script looks like this and is ready to run with cscript or wscript from elevated or non elevated mode, the one thing I didn’t bother fixing in the end was to output the text from the elevated process to the first process if it’s restarted as elevated:

If nothing else, I hope it could at least serve as an example of some scripting techniques.


Edit (5 minutes later):
Forgot to credit the sources from which most of the above was combined, fixing the path here and running elevated here.