In fact, I wrote this print environment variables in PowerShell tutorial after getting annoyed from echo and set commands output. Additionally, You can see readability issue with the list all environment variables from the command line tutorial outputs.
Print Environment Variables in PowerShell
Specifically, You will love the strength of PowerShell when writing PowerShell scripts or small code snippets. To illustrate, I am writing few example commands that you can use in PowerShell to list all environment variables.
Environment Variable in PowerShell
Firstly, You can use $env: variable in PowerShell to list all environment variables in PowerShell. Further, You cannot use echo %PATH%
command in PowerShell because it will only display %PATH% in console. Of course, we have different PowerShell syntax to display environment variables paths, here is the command.
echo $env:Path
Of course, you can ask that what is the difference between Command Prompt and PowerShell output? Well, I displayed the results before formatting the output.
Split Semicolon Separated String with PowerShell
Let’s start to check the power of PowerShell and I am using Windows PowerShell ISE for easiness. Similarly, You can see the below output from the echo $env:Path.Split(";")
command.
$env:Path.Split(";")
PowerShell Commands to List Environment Variables
Further, I am writing few other PowerShell commands to list environment variables.
echo $env:Path Command
Firstly, You can use the echo command in PowerShell but I called it extra code because you can directly print the variables in PowerShell.
echo $env:Path
Get-Item env:Path Command
Secondly, If you are coding lover and love to write more code to do the task, you can use Get-Item env:Path Command for this.
Get-Item Env:\Path | ForEach-Object { $name = $_.Name $_.Value -split ';' | Where-Object { $_ } | Select-Object @{n="Name";e={$name}}, @{n="Value";e={$_}} }
Get-ChildItem env:Path Command
Thirdly, You can use Get-ChildItem env:Path Command command to do the same but with some extra code. You will find it similar to Get-Item env:Path Command.
Get-ChildItem Env:\Path | ForEach-Object { $name = $_.Name $_.Value -split ';' | Where-Object { $_ } | Select-Object @{n="Name";e={$name}}, @{n="Value";e={$_}} }
Variables in PowerShell
Additionally, you can see that we used dollar sign ($) as our starting command, it is called variable in PowerShell.