Using VBS to set the environment variables

I use windows and I hate when I have to switch on another computer, or when I have to do any change related to windows especially changing the environment variables. I like to download the tool, framework, … I use as a zip and then to do minimal operations to install it. The thing I hate the most is setting environment variables, and path in that small dialog window. I have all my java applications and frameworks in one single directory so the configuration depends only on the thing I hate most: thats right, environment variables.

Now I decided to move them all in one single file, and I’m using Vbs for that. Don’t hurry to blame me for that, I’m a java programmer but in this case is the best solution. If you take a look on wikipedia you’ll see that “VBScript is installed by default in every desktop release of the Windows Operating System (OS) since Windows 98”. That’s all I need to know about VBScript to make me use it to create the script that will help me to put my java tools on every windows computer I can access, without having to add the env variables manually(muahahahahaa). Unfortunately the computer requires a restart.

Now, lets cut the chat. Here is the script(maven-env.vbs) which sets the environments required by maven2 according to the instructions and add the bin folder to the class path:

Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")

WScript.Echo "The current PATH is:" & chr(10) & chr(10) & Replace(WSHShell.Environment.item("PATH"),";", chr(10))

WScript.Echo "Current values of the variables to be changed or added:" & chr(10) _
	& "M2_HOME=" & WSHShell.Environment.item("M2_HOME") & chr(10) _
	& "M2=" & WSHShell.Environment.item("M2")
	
'maven environment variables are created according to http://maven.apache.org/download.html#Installation
WSHShell.Environment.item("M2_HOME") = "C:\java\apache-maven-2.0.9"
WSHShell.Environment.item("M2") = "%M2_HOME%\bin"
WSHShell.Environment.item("PATH") = WSHShell.Environment.item("path") & ";%M2%"

Set WSHShell = Nothing    
WScript.Quit(0)

I’m using the first echo line in a separate script to display the path and to display one directory on each line replacing ; with end of line(chr(0) represents end of line). If you want to change registry values you can use:

WSHShell.RegWrite "HKCU\path\key", "value"
WSHShell.RegRead("HKCU\path\key")

If you need to know that script for more advanced operation you can check VBScript Functions

Did you enjoy this tutorial? Be sure to subscribe to the my RSS feed not to miss my new posts!
... or make it popular on

1 Comments

Leave a Comment.