当前位置:网站首页>[PowerShell] command line output and adding system environment variables

[PowerShell] command line output and adding system environment variables

2022-06-12 05:53:00 Amos-Chen

View a system variable

Q: How to use Windows PowerShell View my environment variables Path, Which folders are there and the order in which they are displayed ?

A: Use $ env And get the Path The value of the variable . By default , It is displayed as a continuous string , It may be a little hard to read . The order is as follows :

$env:path

however , If you use –split Operator breaks a string at a semicolon , It is more readable :

$env:path -split“;”

 Insert picture description here
other
such as : Check the system

$env:os

such as : System cores CPU

$env:NUMBER_OF_PROCESSORS

Be careful : View these environment variables , It must exist in the environment variable , Or you added it yourself .

Modify environment variables

First understand a thing : stay Windows On , You can define environment variables in three scopes :

  1. System level (Machine)
  2. User level (User)
  3. Session level (Process), Which means turn it off powershell Is the failure , This list of variables is inherited from the parent process , And it's based on Machine and User Variables in the scope are constructed .

user & System & Session level

 Insert picture description here
In the picture :· User variables Corresponding user level scope , System variables Corresponding to the system level scope .

Add... On the basis of the original environment variables , And specify the scope ( user or System or conversation ), One command is done :

$addPath=‘c:\add\you\path\here’; $target=‘User’ ; $path = [Environment]::GetEnvironmentVariable(‘Path’, $target); $newPath = $path + ‘;’ + $addPath; [Environment]::SetEnvironmentVariable(“Path”, $newPath, $target)

Be careful :

  1. $addPath The equal sign is followed by the path you want to add
  2. $target The equal sign is followed by the scope ,Machine( System level ) / User( User level ) / Process( Session level ) Choose one of three , The first two amendments will take effect in time , And turn off powershell The conversation is still valid .
  3. Other places don't need to be changed , Direct execution

Session level modification - Method 2

Add a new environment variable

$env:amostest=“amos for test desc”

Add... To the existing environment variables

$env:path+=";c:\your_path"

Reference resources

About Environment Variables
EnvironmentVariableTarget Enum

原网站

版权声明
本文为[Amos-Chen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010614149695.html