Peter Hinchley

How to Query an Environment Variable on a Remote Computer

Tagged: windows, vbscript, wmi

I often need to find the value of a system environment variable on a remote computer. Fortunately this is easily achieved through the use of a WMI query.

The following VBScript accepts the name of the target computer as a command line parameter. It then initiates a WMI query against this computer, requesting the value of a variable called MyVar. To alter the variable that is queried, change the value of VARNAME in the script.

To use the script, save the code into a file named C:\Temp\getvar.vbs. Open a command prompt, navigate to C:\Temp, and enter the following command:

cscript.exe getvar.vbs computername

Where computername is the name of the target computer.

The script will output the value of the variable to the console window.

VARNAME = "MyVar"

Set oArgs = WScript.Arguments
Set oWMIService = GetObject("winmgmts:\\" & oArgs(0) & "\root\cimv2")

Set cItems = oWMIService.ExecQuery("Select * From Win32_Environment Where Name = '" & VARNAME & "'")

For Each oItem In cItems
  WScript.Echo "Value of " & VARNAME & ": " & oItem.VariableValue
Next

Your Say