WshShell.Environment

Return a Windows Environment variable.
Syntax
WshShell.Environment([strType])

Key:
strType is one of
"System" (HKLM),
"User" (HKCU),
"Volatile" or "Process"

Example

'Write to env variables
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Environment("USER").Item("MyVar1") = "hello"
WshShell.Environment("USER").Item("MyVar2") = "world"

'Read Env Variables
WScript.Echo WshShell.Environment("USER").Item("MyVar1")
WScript.Echo WshShell.Environment("USER").Item("MyVar2")

' Retrieve the %COMPUTERNAME% system environment variable
WScript.Echo WshShell.Environment("PROCESS").Item("COMPUTERNAME")

".Item" is actually the default property and can be omitted

WshShell.ExpandEnvironmentStrings

Expand a Windows environment variable.
Syntax
WshShell.ExpandEnvironmentStrings(strString)

Key:
strString is a PROCESS environment variable
(enclosed with "%" characters)

Example

MsgBox "Prompt is " & WshShell.ExpandEnviromentStrings("%PROMPT%")

WshShell.Environment.Remove

Remove a Windows environment variable.
Syntax
WshShell.Environment(strType).Remove(strName)

Arguments:
strString is the environment variable
to be removed

strType is one of
"System" (HKLM),
"User" (HKCU),
"Volatile" or "Process"

Example

' Delete the LAST_LOGIN_DATE user environment variable
Set WshShell = Wscript.CreateObject("Wscript.Shell")

WshShell.Environment("USER").Remove("LAST_LOGIN_DATE")

Note: for anything other than USER variables - the user needs permissions to delete the variable. Environment variables removed with the Remove method are not removed permanently; they are only removed for the current session. 

Make a Free Website with Yola.