Dealing with Environment Variables

by Jun 25, 2014

To read a Windows environment variable in PowerShell, simply use the prefix "env:":

PS> $env:windir
C:\Windows

PS> $env:USERNAME
Tobias

Actually, "env:" is a drive, so you can use it to find all (or some) environment variables. This would list all environment variables that have "user" in its name:

PS> dir env:\*user*

Name Value
---- -----
USERPROFILE C:\Users\Tobias
USERNAME Tobias
ALLUSERSPROFILE C:\ProgramData
USERDOMAIN TobiasAir1

Twitter This Tip! ReTweet this Tip!