Introduction
PowerShell is a command-line tool on the Windows platform, which can easily find, add, modify and delete environment variables. In this article, LZ will introduce how to do these operations in PowerShell.
Find environment variables
Finding environment variables in PowerShell is very easy. Just use Get-ChildItem
the command Env:
as the path.
1. View the current user environment variables
For example, to find an environment variable named PATH, you can use the following command:
Get-ChildItem Env:PATH
This command will display the name and value of the environment variable. If you want to display only the value of the environment variable, you can use the following command:
$env:PATH
2. View system environment variables
Use the following command to view the environment variables of the system ( 注意
: powershell version is too low to support -Scope
this parameter):
Get-ChildItem Env: -Scope Machine
Add environment variables
To add a new environment variable, use New-Item
the command with Env:
path as the path, and specify the environment variable name and value to be added. For example, to C:\Program Files\MyApp
add to the PATH environment variable, you can use the following command:
$env:PATH += ";C:\Program Files\MyApp"
This command will add a new path to the end of the PATH environment variable.
Modify environment variables
To modify an existing environment variable, use Set-Item
the command with the Env:
path as the path, and specify the name of the environment variable to be modified and the new value. For example, to C:\Program Files\MyApp
replace with D:\MyApp
, you can use the following command:
$env:PATH = $env:PATH -replace "C:\\Program Files\\MyApp", "D:\\MyApp"
This command will look for all paths in the PATH environment variable C:\Program Files\MyApp
and replace them with D:\MyApp
.
delete environment variable
To delete an existing environment variable, use Remove-Item
the command, with Env:
the path as the path, and specify the name of the environment variable to be deleted. For example, to remove TEST_VAR
an environment variable named , the following command can be used:
Remove-Item Env:TEST_VAR
This command will remove TEST_VAR
the environment variable named .
注意
: Please be careful when deleting environment variables to ensure that the deleted environment variables will not affect the normal operation of the system or applications.
Automatically add environment variables
If you need to add the same environment variable on multiple computers, you can automate the process through scripts. Here is an example script that adds C:\Program Files\MyApp to the PATH environment variable:
$existingPath = [Environment]::GetEnvironmentVariable("Path", "Machine")
$newPath = $existingPath + ";C:\Program Files\MyApp"
[Environment]::SetEnvironmentVariable("Path", $newPath, "Machine")
This script will first get the current PATH environment variable value, then C:\Program Files\MyApp
append to the end, and use SetEnvironmentVariable
the method to set the new value back to the environment variable.
Summarize
Finding, adding, and modifying environment variables is easy in PowerShell. Use Get-ChildItem
commands to find environment variables, use New-Item
commands to add new environment variables, use Set-Item
commands to modify existing environment variables, and use Remove-Item
commands to delete environment variables. Of course, these operations can be easily automated using scripts to manage environment variables more efficiently.
Conclusion: Be kind to yourself, tolerate your own shortcomings and mistakes, and learn to learn from them