Add or modify an environmental variable (BAT, VBS)

Purpose: Add or modify a windows OS environmental variable from command-line

It is not very unusual that an application requires an environmental variable to function properly. Depending on how deep into the application packaging you must dive, sooner or later you will be face to face with an environmental variable application. Most common scenarios is that something will work a lot easier with a custom environmental variable or that the actual windows PATH variable must be modified to include the new application.

The most important theory is the following:

User environmental variables: HKEY_CURRENT_USER\Environment
System enviromental variables: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

What ever you do, make sure if you modify or recreate the PATH-variable that the registry data type is REG_EXPAND_SZ! Sh*t will not work if it is just the reqular REG_SZ, the whole path variable will collapse. Not a single path in the PATH-variable will work until this is corrected. Scripting with VBS will change the REG_EXPAND_SZ as default to REG_SZ. Thats why your script will FAIL unless you do steps to correct the data type to REG_EXPAND_SZ (or take that into account in the first place).

Usage: cscript.exe IT_APP_VBS_ModifyAnEnvironmentalVariable.vbs
Configuration: You must change your own variable or path value into the script

Modify a PATH environmental variable (VBS)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
' *********************************************************************************
' ** Script Name: IT_APP_VBS_ModifyAnEnvironmentalVariable.vbs
' ** Created on: 04.10.2013
' ** Author: Jyri Lehtonen / http://it.peikkoluola.net
' **
' ** Purpose: Add or modify a windows OS environmental variable from command-line
' ** Usage: IT_APP_VBS_ModifyAnEnvironmentalVariable.vbs
' **
' ** License: This program is free software: you can redistribute it and/or modify
' ** it under the terms of the GNU General Public License as published by
' ** the Free Software Foundation, either version 3 of the License, or
' ** (at your option) any later version.
' **
' ** This program is distributed in the hope that it will be useful,
' ** but WITHOUT ANY WARRANTY; without even the implied warranty of
' ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' ** GNU General Public License for more details.
' ** 
' ** History: 
' ** 1.0 / Jyri Lehtonen / 04.10.2013 / Initial version.
' *********************************************************************************
 
'Modify PATH environmental variable (VBS)
strPath = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
'Clear old value PATH variablee (if that exists)
strPath=Replace(strPath,"REPLACE_OLD_VALUE","")
'Creat a new PATH environmental variable value
strPath=""&strPath&"INSERT_NEW_VALUE"
'Add the new PATH variable to the registry
WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path", strPath
' *********************************************************************************
' ** Script Name: IT_APP_VBS_ModifyAnEnvironmentalVariable.vbs
' ** Created on: 04.10.2013
' ** Author: Jyri Lehtonen / http://it.peikkoluola.net
' **
' ** Purpose: Add or modify a windows OS environmental variable from command-line
' ** Usage: IT_APP_VBS_ModifyAnEnvironmentalVariable.vbs
' **
' ** License: This program is free software: you can redistribute it and/or modify
' ** it under the terms of the GNU General Public License as published by
' ** the Free Software Foundation, either version 3 of the License, or
' ** (at your option) any later version.
' **
' ** This program is distributed in the hope that it will be useful,
' ** but WITHOUT ANY WARRANTY; without even the implied warranty of
' ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' ** GNU General Public License for more details.
' ** 
' ** History: 
' ** 1.0 / Jyri Lehtonen / 04.10.2013 / Initial version.
' *********************************************************************************

'Modify PATH environmental variable (VBS)
strPath = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
'Clear old value PATH variablee (if that exists)
strPath=Replace(strPath,"REPLACE_OLD_VALUE","")
'Creat a new PATH environmental variable value
strPath=""&strPath&"INSERT_NEW_VALUE"
'Add the new PATH variable to the registry
WshShell.RegWrite "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path", strPath
Modify a PATH environmental variable (VBS)
1
2
3
4
5
6
7
8
9
10
11
12
'Modify the PATH environmental variable (VBS)
strPath = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
'Remove duplicate entry, if exists.
strPath=Replace(strPath,";REPLACE_OLD_VALUE","")
'Set new PATH variable
strPath=""&strPath&";%NEW_VARIABLE%"
'Remove double ";;", if exists
strPath=Replace(strPath,";;",";")
strPath=Replace(strPath,";;;",";")
 
'Write the new value to registry
WshShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path", strPath
'Modify the PATH environmental variable (VBS)
strPath = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
'Remove duplicate entry, if exists.
strPath=Replace(strPath,";REPLACE_OLD_VALUE","")
'Set new PATH variable
strPath=""&strPath&";%NEW_VARIABLE%"
'Remove double ";;", if exists
strPath=Replace(strPath,";;",";")
strPath=Replace(strPath,";;;",";")

'Write the new value to registry
WshShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path", strPath
Add a new environmental variable (BAT)
1
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "VARIABLE NAME" /t "REG_SZ" /d "VARIABLE VALUE" /f
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "VARIABLE NAME" /t "REG_SZ" /d "VARIABLE VALUE" /f
Add a new environmental variable (VBS)
1
2
3
4
' Add a new Environmental Variable (VBS)
NEW_VARIABLE_VALUE = "variable path or value"
set WshShell = CreateObject("WScript.Shell")
WshShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\NEW_VARIABLE", NEW_VARIABLE_VALUE
' Add a new Environmental Variable (VBS)
NEW_VARIABLE_VALUE = "variable path or value"
set WshShell = CreateObject("WScript.Shell")
WshShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\NEW_VARIABLE", NEW_VARIABLE_VALUE
Add a new environmental variable (BAT)
1
2
3
4
5
6
7
8
REM Add a new Environmental Variable (BAT)
 
setx /M NEW_VARIABLE NEW_VARIABLE_VALUE
 
set V1=%PATH%
set V2=%V1%;%NEW_VARIABLE%
 
setx /M Path "%V2%"
REM Add a new Environmental Variable (BAT)

setx /M NEW_VARIABLE NEW_VARIABLE_VALUE

set V1=%PATH%
set V2=%V1%;%NEW_VARIABLE%

setx /M Path "%V2%"
Modify an environmental variable (BAT)
1
2
3
4
5
6
'Modify the PATH environmental variable (BAT)
set Var1=%PATH%
set Var2=PATH_VALUE_YOU_WANT_TO_ADD;
set Var3=%Var2%%Var1%
 
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%Var3%" /f
'Modify the PATH environmental variable (BAT)
set Var1=%PATH%
set Var2=PATH_VALUE_YOU_WANT_TO_ADD;
set Var3=%Var2%%Var1%

REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%Var3%" /f

Leave a Reply

Your email address will not be published. Required fields are marked *


+ four = seven

Post Navigation