I need to use a VBS script to set the environment variable %computername% to "blah". This seems like a simple enough task but for some reason I'm finding it difficult this morning. I can do this via a command prompt (set computername=blah) and I can get the value of the environment variable via VBS but I cant seem to find the correct syntax to override the environment variable. Any help would be greatly appreciated. Thanks!
Asked
Active
Viewed 688 times
1 Answers
1
How you create it determines its type and lifetime. If you want the variable to be available until the user logs off, you can create it as VOLATILE
.
Set objShell = CreateObject("WScript.Shell")
objShell.Environment("VOLATILE")("MyVariable") = "This is some data to share"
And to read it back...
strValue = objShell.Environment("VOLATILE")("MyVariable")
Other options for the variable's type are SYSTEM
, USER
, and PROCESS
. See here for a nice article describing the differences.

Bond
- 16,071
- 6
- 30
- 53
-
That seems really close to what i'm needing, but when I pull up a command prompt and type echo %computername% I still get the name of my computer. Is there a way to override that variable? – msindle Sep 15 '14 at 15:19
-
Are you trying to change the name of the computer? Because changing the environmental variable is not the way to go about it. I just did `objShell.Environment("PROCESS")("COMPUTERNAME") = "NEW"` and it worked. – Bond Sep 15 '14 at 15:24
-
well like I said if i open a command prompt i can type set computername=blah and then if i do an echo %computername% it prints "blah" I need to do that same thing just with a VBS script. I'm using thinapp and one of the programs that I have thinapp'd looks at the computer, so if i can trick it into thinking its on the old computer I think it should help solve a lot of problems – msindle Sep 15 '14 at 15:28
-
I just did some more testing and it looks like you have to set both the `SYSTEM` and `USER` ones. And then it takes a few seconds but the variable is updated. – Bond Sep 15 '14 at 15:30
-
what did you use for "MyVariable", i'm using computername? – msindle Sep 15 '14 at 15:33
-
Right. I used `objShell.Environment("SYSTEM")("COMPUTERNAME") = "NEW"`, followed by `objShell.Environment("USER")("COMPUTERNAME") = "NEW"` and it updated my `COMPUTERNAME` variable. – Bond Sep 15 '14 at 15:36