≡ Menu

Query environment variables remotely using PowerShell

The script discussed in this article will help you to query environment variable values on remote windows computer using PowerShell.

Environment variables on a computer can be accessed via  Env:\ PSdrive or [System.Environment] dotnet class locally. None of these methods can be used to query environment variables remotely. Only exception is, you can use one of these methods through PowerShell remoting to get environment variable information. However that is not true remote query and doesn’t work in cases where remoting not available plus systems where PS is not installed.

In such cases, WMI will come handy to query this information remotely. Win32_Environment WMI class has information about environmental variables and querying the instances of this class will give us the desired output. My script is a wrapper around this WMI class to return the environment variable values.

This script takes two arguments. 1) ComputerName and 2) Name. While the computer name represents name of the computer from which you want to query environment variables and Name parameter is the name of environment variable you want to query. Both of these are optional parameters.


 [string[]]$ComputerName =$env:ComputerName,            
foreach($Computer in $ComputerName) {            
 Write-Verbose "Working on $Computer"            
 if(!(Test-Connection -ComputerName $Computer -Count 1 -quiet)) {            
  Write-Verbose "$Computer is not online"            
 try {            
  $EnvObj = @(Get-WMIObject -Class Win32_Environment -ComputerName $Computer -EA Stop)            
  if(!$EnvObj) {            
   Write-Verbose "$Computer returned empty list of environment variables"            
  Write-Verbose "Successfully queried $Computer"            
  if($Name) {            
   Write-Verbose "Looking for environment variable with the name $name"            
   $Env = $EnvObj | Where-Object {$_.Name -eq $Name}            
   if(!$Env) {            
    Write-Verbose "$Computer has no environment variable with name $Name"            
  } else {            
   Write-Verbose "No environment variable specified. Listing all"            
 } catch {            
  Write-Verbose "Error occurred while querying $Computer. $_"            


.\Get-Environmentvariable.ps1 -ComputerName TESTPC1

This returns the list of environment variables on TESTPC1 computer.

.\Get-EnvironmentVariable.ps1 -ComputerName TESTPC1 -Name PATH

This returns the environment variable that matches the name PATH

I have also made this script available in TechNet Script Gallery.

{ 5 comments… add one }
  • Richard W January 19, 2015, 5:17 pm

    Hi. Is it possible to use this script to query multiple computers either via a text list or a variable containing the output of a get-adcomputer cmd?


    • TechiBee January 25, 2015, 3:49 pm

      HI Richard, Yes you can do that. See below

      .\Get-EnvironmentVariable.ps1 -ComputerName (get-content c:\temp\servers.txt)

  • Mike March 16, 2016, 9:48 pm

    Is there a way to get this script to show the entire path variable instead of truncating it?

    • Wintel Rocks March 23, 2016, 11:08 pm

      Hi Mike, Can you post a screenshot showing what is being truncated?

  • SteveA May 13, 2016, 11:29 pm

    This is a nice help, thanks for posting! Mike, I think this will help with your truncation issue:

    .\Get-EnvironmentVariable.ps1 -ComputerName -Name PATH | ft -auto -wrap

Leave a Comment