Using Wildcards with Environment Variables

by Nov 9, 2011

You can use the env: PowerShell drive to list all Windows environment variables like this:

PS> Dir env:*user*

Name                           Value
----                           -----
USERNAME                       w7-pc9
USERPROFILE                    C:\Users\w7-pc9
ALLUSERSPROFILE                C:\ProgramData
USERDOMAIN                     DEMO5

To access the content of an individual environment variable, use the variable syntax like this:

"Username: $env:username"

And if you want to list environments with a keyword in its value, use Where-Object. This will list all environment variables with a "program" keyword in its value:

PS> dir env: | Where-Object { $_.Value -like '*Program*' }

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
CLASSPATH                      .C:\Program Files (x86)\QuickTime\QTSystem\Q...
CommonProgramFiles             C:\Program Files\Common Files
CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
CommonProgramW6432             C:\Program Files\Common Files
...

Twitter This Tip!
ReTweet this Tip!