How to find, add, modify and delete environment variables in PowerShell: Solve the problem that the command fails after manually setting environment variables

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-ChildItemthe 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

insert image description here

2. View system environment variables

Use the following command to view the environment variables of the system ( 注意: powershell version is too low to support -Scopethis parameter):

Get-ChildItem Env: -Scope Machine

Add environment variables

To add a new environment variable, use New-Itemthe command with Env:path as the path, and specify the environment variable name and value to be added. For example, to C:\Program Files\MyAppadd 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-Itemthe 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\MyAppreplace 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\MyAppand replace them with D:\MyApp.

delete environment variable

To delete an existing environment variable, use Remove-Itemthe command, with Env:the path as the path, and specify the name of the environment variable to be deleted. For example, to remove TEST_VARan environment variable named , the following command can be used:

Remove-Item Env:TEST_VAR

This command will remove TEST_VARthe 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\MyAppappend to the end, and use SetEnvironmentVariablethe method to set the new value back to the environment variable.

Summarize

Finding, adding, and modifying environment variables is easy in PowerShell. Use Get-ChildItemcommands to find environment variables, use New-Itemcommands to add new environment variables, use Set-Itemcommands to modify existing environment variables, and use Remove-Itemcommands 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

Guess you like

Origin blog.csdn.net/Da_zhenzai/article/details/130238775