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
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:
$Env:Path.Split(';')
myself (output one directory per line).
Jan 3, 2019 at 21:15
-split
is a regex operator, String.Split()
is not
Jul 11, 2019 at 9:40
get-alias
lists more, including gal
(which can be found with get-alias | where {$_.Name -like '*al*'}
, furthermore gal | gm
shows all property names where
can filter on. get-command
(gcm
), get-help
(help
), help gcm
, and help help
, may also be helpful.
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 works best, in my opinion:
Get-Item Env:PATH
Get-ChildItem
(There's no hierarchy with environment variables).Set-Item -Path env:SomeVariable -Value "Some Value"
)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 lets 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 a specific "drive", and the colon is the familiar syntax to anyone who's worked with Windows.
You can traverse through the drives like this:
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(";")
As a complementary to other answers,
In the case of listing all Environment Variables, to concise the commands, you can use dir
, ls
, and gci
(an acronym for Get-ChildItem) as they are three aliases of Get-ChildItem
.
So, all of the following commands are equal and can be used in this context:
Get-ChildItem Env:
or
dir Env:
or
ls Env:
or
gci Env:
More information in shellgeek.
Also, as a practical use case, you can get an alphabetically sorted list of Environment Variables based on Name
or Value
like this:
Get-ChildItem Env: | Sort Name
or
Get-ChildItem Env: | Sort Value
FYI: You cannot replace Get-ChildItem Env:
with Get-Item Env:
in the above commands (The list won't be sorted and remains unchanged). So, in such cases, the Get-ChildItem
might be preferred to the Get-Item
mentioned in this answer by BitBite.
Finally, you can sort the value of a specific record of Environment Variables like this:
(Get-ChildItem Env:PATH).Value.Split(';') | Sort
echo $env:Path