Skip to content

Instantly share code, notes, and snippets.

@adojos
Last active March 9, 2023 11:03
Show Gist options
  • Save adojos/414c0f611ea4ae8410227c9b119caf93 to your computer and use it in GitHub Desktop.
Save adojos/414c0f611ea4ae8410227c9b119caf93 to your computer and use it in GitHub Desktop.
VBS: Write Windows Env Variables #vbs
Sub WriteEnvVar(strEnvVarType, strEnvVarName, strEnvVarValue, strWriteType)
Dim strOldVarValue, strNewVarValue
Dim strVarType
Set objShell = CreateObject("WScript.Shell")
Set objEnv = objShell.Environment(strEnvVarType) ' envVarType= System or User
Select Case strWriteType
Case strWriteTypeReplace
strOldVarValue = objEnv(strEnvVarName)
strNewVarValue = strEnvVarValue
objEnv(strEnvVarName) = strNewVarValue
WScript.StdOut.WriteBlankLines(1)
WScript.StdOut.WriteLine "SETTING " & strEnvVarName & " TO " & strNewVarValue & " ... DONE!"
WScript.StdOut.WriteBlankLines(1)
Case strWriteTypeAddNew
objEnv(strEnvVarName) = strEnvVarValue
WScript.StdOut.WriteBlankLines(1)
WScript.StdOut.WriteLine "CREATING " & strEnvVarName & " AND SETTING TO " & strEnvVarValue & " ... DONE!"
WScript.StdOut.WriteBlankLines(1)
End Select
Set objEnv = Nothing
Set objShell = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment