ChatGPT解决这个技术问题 Extra ChatGPT

How to print environment variables to the console in PowerShell?

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

h
hagrawal

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:

I like $Env:Path.Split(';') myself (output one directory per line).
For sure, or $env:Path -split ';'
@AmirKatz The output is the same, because the two operations do the exact same thing :) -split is a regex operator, String.Split() is not
for short use gci env:
@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.
n
not2qubit

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.


B
BitBite

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

z
zachbugay

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