This article describes how to get the value of an environment variable in PowerShell. You can list all environment variables, or you can specify the value of the environment variable by the name of the variable.
In the development process, often need to use environment variables (such as the current computer name, login username, PATH environment variables, etc.), then how to know in the PowerShell of the environment variables? And how do you get the value of the specified environment variable?
PowerShell through the environment variable provider (environment Provider) gives us access to environment variables. By default, PowerShell creates a drive (named Env) to deal with environment provider. Therefore, we can use the Env this drive to handle the operation related to environment variables.
1. List all environment variables
We can use "Get-childitem env:" To get a list of all the environment variables. The results of the operation of the small knitting machine are as follows:
Copy Code code as follows:
PS c:\users\zhanghong> dir env:
Name Value
---- -----
AllUsersProfile C:\ProgramData
AppData C:\Users\zhanghong\AppData\Roaming
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles (x86) C:\Program files (x86) \common files
COMPUTERNAME Zhanghong-book
ComSpec C:\Windows\system32\cmd.exe
Homedrive C:
HomePath \users\zhanghong
Java_home D:\JavaDevEnv\jdk1.6.0_16
......
Note that Get-childitem and Dir are a means, the latter being the alias of the former. Hongo likes to be lazy, so he uses dir directly.
All of the environment variables are listed above, and interested friends can familiarize themselves with them so that they are invoked later when they need to use the value of the variable.
2, get the value of environment variables
Copy Code code as follows:
Syntax: $env:< variable name >
For example, if I want to get the current computer name, the usage is as follows:
Copy Code code as follows:
PS c:\users\zhanghong> $env: ComputerName
Zhanghong-book
Note that the environment variable is also a variable, so you must have a dedicated prefix "$" for the PowerShell variable before "env:".
About PowerShell to get the value of the environment variables, this article on the introduction so much, I hope to help you, thank you!