I'm starting to use PowerShell and am trying to figure out how to echo
a system environment variable to the console to read it.
Neither of the below are working. The first just prints %PATH%
, and the second prints nothing.
echo %PATH%
echo $PATH
echo $env:Path
Prefix the variable name with env
:
$env:path
For example, if you want to print the value of environment value "MINISHIFT_USERNAME", then command will be:
$env:MINISHIFT_USERNAME
You can also enumerate all variables via the env
drive:
Get-ChildItem env:
In addition to Mathias answer.
Although not mentioned in OP, if you also need to see the Powershell specific/related internal variables, you need to use Get-Variable
:
$ Get-Variable
Name Value
---- -----
$ name
? True
^ gci
args {}
ChocolateyTabSettings @{AllCommands=False}
ConfirmPreference High
DebugPreference SilentlyContinue
EnabledExperimentalFeatures {}
Error {System.Management.Automation.ParseException: At line:1 char:1...
ErrorActionPreference Continue
ErrorView NormalView
ExecutionContext System.Management.Automation.EngineIntrinsics
false False
FormatEnumerationLimit 4
...
These also include stuff you may have set in your profile startup script.
The following is works best in my opinion:
Get-Item Env:PATH
It's shorter and therefore a little bit easier to remember than Get-ChildItem. There's no hierarchy with environment variables. The command is symmetrical to one of the ways that's used for setting environment variables with Powershell. (EX: Set-Item -Path env:SomeVariable -Value "Some Value") If you get in the habit of doing it this way you'll remember how to list all Environment variables; simply omit the entry portion. (EX: Get-Item Env:)
I found the syntax odd at first, but things started making more sense after I understood the notion of Providers. Essentially PowerShell let's you navigate disparate components of the system in a way that's analogous to a file system.
What's the point of the trailing colon in Env:
? Try listing all of the "drives" available through Providers like this:
PS> Get-PSDrive
I only see a few results... (Alias, C, Cert, D, Env, Function, HKCU, HKLM, Variable, WSMan). It becomes obvious that Env
is simply another "drive" and the colon is a familiar syntax to anyone who's worked in Windows.
You can navigate the drives and pick out specific values:
Get-ChildItem C:\Windows
Get-Item C:
Get-Item Env:
Get-Item HKLM:
Get-ChildItem HKLM:SYSTEM
I ran across this myself. I wanted to look at the paths but have each on a separate line. This prints out the path, and splits it by the semicolon.
$env:path.Split(";")
Success story sharing
$Env:Path.Split(';')
myself (output one directory per line).$env:Path -split ';'
-split
is a regex operator,String.Split()
is notgci env:
get-alias
lists more, includinggal
(which can be found withget-alias | where {$_.Name -like '*al*'}
, furthermoregal | gm
shows all property nameswhere
can filter on.get-command
(gcm
),get-help
(help
),help gcm
, andhelp help
, may also be helpful.