Conventional consoles generally do not have such advanced variable systems as PowerShell. They are all dependent on the environment variables of the machine itself to operate. Environment variables are important to PowerShell because they cover a lot of details about the operating system. In addition, the variables in PowerShell only exist within the PowerShell session, and once the PowerShell is closed, the variables die. However, if the environment variable is updated, it will continue to be saved in the operating system, even if other programs can call it.
Read a special environment variable
Read the installation path of the Windows operating system through environment variables, and the installation path for the default application.
Ps> $env: windir
C:\Windows
ps> $env:P rogramfiles
C:\Program Files
Through $env: This prompts PowerShell to ignore the basic variable: drive, but instead go to the environment variable env: drive to find the variable. In order to be consistent with other variables, PowerShell environment variables can also be used like other variables. For example, you can insert it into the text.
Ps> ' My Computer name $env: COMPUTERNAME ' my
computer name myhome-test-01
Find environment variables
PowerShell keeps records of all environment variables in the env: virtual drive, so you can list all environment variables. Once you have identified the name of the environment variable, you can use $env:name access.
ps> ls env:
Name Value
---- -----
allusersprofile C:\ProgramData
appdata c:\ User\sv-test\home\appdata\roaming
commonprogramfiles C:\Program Files\Common Files
COMPUTERNAME myhome-test-01
ComSpec C:\Windows\system32\cmd.exe
fp_no_host_check NO
homedrive C:
HomePath Users\v-test\home
Create a new environment variable
To create a new environment variable, just as you would for creating other variables, you only need to specify the env: virtual drive
Ps> $env: testvar1= "This is my environment variable"
ps> $env: testvar2= "Hollow, environment variable"
ps> ls env:test*
Name Value
---- -----TestVar1 This are my environment variable
TestVar2 Hollow, environment variable
Deleting and updating environment variables
Delete and update environment variables and general variables in PowerShell. For example, to delete the windir in an environment variable,
Ps> del env:windir
ps> $env: windir
ps>
You can update the environment variable $env:os for Linux redhat.
ps> $env: OS
windows_nt
ps> $env: os= "Redhat linux"
ps> $env: OS
Redhat Linux
Would it not be safe to manipulate environment variables directly? In fact, it's safe because the environment variable in $env is just a copy of the machine environment variable, and even if you change it, the next time you reopen it, it's back to the beginning. (except for the. NET method update environment variables)
We can append the list of trusted folders to the end of the environment variable so that the files or scripts under those files can be executed directly from the relative path, or even the extended names can be omitted.
Ps> Md. myscript
Directory:
Mode lastwritetime Length Name
---- ------------- ----------
d---- 2011/11/29 18:20 myscript
ps> cd. MyScript
Psmyscript > "Write-host ' Hollow, Powershell '" > Hollow.ps1
psmyscript> hollow.ps1
,
Hollow Psmyscript> CD ...
ps> $env:P ath+= "; C:powershellmyscript "
ps> hollow.ps1
Hollow, Powershell ps> Hollow Hollow
, Powershell
environment variable Update takes effect
The above actions for environment variables only affect the current PowerShell session and are not updated on the machine.
. net method [Environment]::setenvironmentvariable operation can take effect immediately.
The following example sets the environment variable for the current user, and after testing, reopen PowerShell still exists
ps> [Environment]::setenvironmentvariable ("Path", "; C:\powershellscript", "User")
ps> [Environment]:: GetEnvironmentVariable ("Path", "User")
; c:\powershellscript