Below is a list of different approaches that can be used to get all the environment variables in windows using PowerShell.
Get-ChildItem -Path Env:
(or)
$Env:
(or)
dir env:
(or)
gci env:
(or)
ls env:
Get specific environment variable value
In the below example, the Get-ChildItem command prints the environment variable value for the APPDATA variable.
Get-ChildItem Env:APPDATA
Sort environment variables by name
Sort
cmdlet take the output of the Get-ChildItem
command and sort the list of PowerShell environment variables by the variable name.
Get-ChildItem Env: | Sort Name
List environment variables using a wildcard
In the below example, the Get-ChildItem command prints the environment variable value for the variable name starting with the user string.
Get-ChildItem Env:user*
Copy all environment variables to file
In the below example, Export-CSV writes the output of Get-ChildItem to a given file.
Get-ChildItem env: | sort-object name | Export-Csv -Path E:\env_variables_output.txt -NoTypeInformation