none
WMI query for environment variables RRS feed

  • Question

  • I want to query with the WMI whether a environment variable exists or not. Since in WQL is no "EXISTS" statement I have to find another way. I searched on many Websites for a solution but I didn't find anything.
    Monday, May 13, 2019 11:01 AM

All replies

  • You don't need to use WMI. 

    if(Test-Path $env:USERNAME){
          # variable exisits
    }else{
          # variable does not exist
    }


    \_(ツ)_/

    Tuesday, May 14, 2019 2:27 PM
  • That doesn't work, that just checks whether a directory named "username" exists in the current path.

    What he needs is

    if ($env:username -ne $null) { "variable exists" }

    Thursday, May 16, 2019 1:43 PM
  • That doesn't work, that just checks whether a directory named "username" exists in the current path.

    What he needs is

    if ($env:username -ne $null) { "variable exists" }

    I think you need to learn how PowerShell works.  Test-Path works with environment variables because that is how PowerShell is designed and "env:" is s searchable drive.

    Please take the time to learn how this works.


    \_(ツ)_/

    Thursday, May 16, 2019 4:39 PM
  • Uhm I literally tested this now:

    PS C:\> test-path $env:computername False PS C:\> $env:computername -eq $null False PS C:\> test-path $env:fghfhggfhf Test-Path : Cannot bind argument to parameter 'Path' because it is null. PS C:\> $env:hghjghjghj -eq $null True

    PS C:\> mkdir $env:computername > $nul
    PS C:\> test-path $env:computername
    True



    • Edited by athmanb Friday, May 17, 2019 7:00 AM
    Friday, May 17, 2019 6:44 AM
  • Thanks for your replies guys..I know that there is a way to query environment variables with PS but... I want to apply a GPO to all Systems that do not have a specific environment variable. Therefore I need a WMI-Filter to query all Systems which don't have that variable. Do you have any solutions for that? :D
    Friday, May 17, 2019 7:32 AM
  • I'm not aware of any WMI class that exposes environment variables but maybe you can use the WMI explorer from https://www.bleepingcomputer.com/download/wmi-explorer/ to browse through the classes and try to find one.
    Friday, May 17, 2019 8:35 AM
  • Uhm I literally tested this now:

    PS C:\> test-path $env:computername False PS C:\> $env:computername -eq $null False PS C:\> test-path $env:fghfhggfhf Test-Path : Cannot bind argument to parameter 'Path' because it is null. PS C:\> $env:hghjghjghj -eq $null True

    PS C:\> mkdir $env:computername > $nul
    PS C:\> test-path $env:computername
    True



    You have to use the correct syntax:

    PS D:\scripts> Test-Path env:computername
    True
    PS D:\scripts> Test-Path env:xxxxxxxxxxx
    False

    The correct way to reference any drive does not include a $.

    Test-Path c:

    try this:

    Get-ChildItem env:

    and this:

     Get-PsProvider environment

    "env:computername" is a path.  "$env:Computername" is a variable.


    \_(ツ)_/

    Friday, May 17, 2019 10:00 AM
  • I'm not aware of any WMI class that exposes environment variables but maybe you can use the WMI explorer from https://www.bleepingcomputer.com/download/wmi-explorer/ to browse through the classes and try to find one.

    WMI exposes system environment is, of all strange place, "Win32_Environment".  What a strange place to put the environment variables wouldn't you say.

    Get-WmiObject win32_environment

    I would have expected it might be in "Win32_ChocolateFactory:.

    When is doubt always ask the system.  It is an expert.

    Get-WmiObject -list *


    \_(ツ)_/

    Friday, May 17, 2019 10:05 AM