PowerShell method of acquiring system environment variable _powershell

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.