Shorter version:
gci env:* | sort-object name
This will display both the name and value.
Answer from jaymjarri on Stack OverflowHow to print all environment variables by scope? e.g. Process, Machine and User
Please help me understand the purpose of environments in PowerShell
env and environment variables in Powershell
Environment Variable inside a single '
How do I list all variables in PowerShell?
To list all variables in your PowerShell session, you can use:
Get-ChildItem Env:
This command will show all currently defined variables in the session, including environment variables.
How to set an environment variable using PowerShell?
To set an environment variable, you can use the following syntax.
$Env:VariableName = “Value”
Replace VariableName with the desired name of your variable and Value with the value you want to assign to it, as example below.
$Env:MY_VAR = “MyValue”
How to check if an environment variable exists in PowerShell?
To check if a specific environment variable exists, you can use the following command.
Test-Path Env:MY_VAR Replace MY_VAR with the name of the environment variable you want to check. If the variable exists, it will return True; if not, it will return False.
Videos
Is there a way to print all environment variables by scope? Ideally I want to be able to create a cmdlet like the following
-
PrintEnvVariables -Scope Machine -
PrintEnvVariables -Scope User -
PrintEnvVariables -Scope Process
This is how I'm setting the environment variables at the moment
Process level
$env:procVar = 'some proc var'
User level
[System.Environment]::SetEnvironmentVariable('myUserVar', 'some user val', [System.EnvironmentVariableTarget]::User)Machine Level
[System.Environment]::SetEnvironmentVariable('myMachvar', 'some mach val', [System.EnvironmentVariableTarget]::Machine)Now I know I can do something like
gci env:
But this just dumps all environment variables from all scopes. I even tried
gci env: | select *
To see if there is any members I can filter by but there doesn't seem to be anything
PSPath : Microsoft.PowerShell.Core\Environment::myUserVar PSDrive : Env PSProvider : Microsoft.PowerShell.Core\Environment PSIsContainer : False Name : myUserVar Key : myUserVar Value : some user val