344

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
2

5 Answers 5

518

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:
6
  • 55
    I like $Env:Path.Split(';') myself (output one directory per line). Jan 3, 2019 at 21:15
  • 6
    For sure, or $env:Path -split ';' Jan 3, 2019 at 21:19
  • 3
    @AmirKatz The output is the same, because the two operations do the exact same thing :) -split is a regex operator, String.Split() is not Jul 11, 2019 at 9:40
  • 8
    for short use gci env:
    – btwiuse
    Jul 17, 2019 at 12:12
  • 2
    @navigaid 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.
    – samus
    Oct 4, 2020 at 18:38
13

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.

13

The following works best, in my opinion:

Get-Item Env:PATH
  1. It's shorter and, therefore, a little easier to remember than Get-ChildItem (There's no hierarchy with environment variables).
  2. The command is symmetrical to one of the ways being used for setting environment variables with Powershell. (EX: Set-Item -Path env:SomeVariable -Value "Some Value")
  3. 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 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
6

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(";")

4

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
2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Oct 19, 2022 at 7:52
  • Thank you, @mohammad. I tried to make this answer clearer. I hope it helps. Oct 19, 2022 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.