Searching for a String in Environmental Variables in Powershell

I’ve updated the post through feedback in the comments:
Regex compare on strings for Get-Variable is Where-Object -Match.

So, the closest parallel to the following bash code:

set | grep "Users"

is the following Powershell:

Get-Variable | Where-Object { $_.Value -Match "Users" }

or shortened:

gv | ? {$_.Value -Match "Users"}

Original Post, with Updates
I’m kind of dumbfounded how this:

Get-Variable | Select-String -Pattern "Users"

returns nothing.


Get-Variable > gv.txt
Get-Content gv.txt | Select-String -Pattern "Users"


$                              Users
HOME                           C:UsersAdministrator
PWD                            C:UsersAdministrator

My initial thought was that maybe redirect captures standard output and standard error and the pipe doesn’t, but then, standard error should all come to the screen unfiltered if not captured by the pipe.

To add insult to injury, apparently

Get-Variable -Include "PS*"

or just

Get-Variable "PS*"

will do a wildcard search for variable names.

Updated, per the comment from @jburger

Get-Variable | Where-Object { $_.Value -ne $null } | Where-Object {$_.ToString().Contains("Users") }


gv | ? { $_.Value -ne $null } | ? {$_.Value.ToString().Contains(“Users”) }

Playing with the Where-Object syntax, I was able to reduce this to:

Get-Variable | Where-Object { $_.Value -ne $null -and $_.ToString().Contains("Users") }


gv | ? { $_.Value -ne $null  -and $_.Value.ToString().Contains(“Users”) }

Definitely, this does the trick. However, I’m disappointed that PowerShell doesn’t automatically cast the object output using a ToString() operation.