How to Remove Environment Variables in PowerShell?

Recently, one user asked me about removing environment variables in PowerShell. PowerShell provides a few different ways to delete environment variables. In this tutorial, I will explain how to remove environment variables in PowerShell.

Environment variables are used to store system-wide values that affect the behavior of processes and applications. They are part of the environment in which a process runs. In Windows, environment variables can be defined at the user or system level.

Sometimes, you may need to remove an environment variable that is no longer needed. Let’s see how to do it.

Types of Environment Variables

  1. User Environment Variables: These are specific to the logged-in user and do not affect other users on the same system.
  2. System Environment Variables: These apply to all users on the system and require administrative privileges to modify.

Note: You should have administrative privileges if you plan to modify system-level variables.

Using the Remove-Item Cmdlet

The simplest way to remove an environment variable in PowerShell is by using the Remove-Item cmdlet.

Here’s an example of deleting a user-level environment variable named “MyVariable”:

Remove-Item Env:\MyVariable

To delete a system-level environment variable, you need to specify the -Path parameter with the Machine scope:

Remove-Item -Path Env:\MyVariable -Scope Machine

By Setting the Variable Value to an Empty String

You can also remove an environment variable in PowerShell by setting its value to an empty string. This way, it removes the variable, like below:

[Environment]::SetEnvironmentVariable("MyVariable", "", "User")

Replace "User" with "Machine" to delete a system-level variable.

Using the Registry

Environment variables are stored in the Windows Registry. So, you can delete the corresponding registry keys, which will indirectly remove the variables.

For user-level variables, delete the key under:

HKEY_CURRENT_USER\Environment

For system-level variables, delete the key under:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Modifying the registry directly can be risky, so it’s recommended to use the PowerShell methods unless absolutely necessary.

Refreshing the Environment

After removing an environment variable, your current PowerShell session will still have the old value cached. To refresh the environment and reflect the changes, you can use the following command:

$env:MyVariable = $null

This clears the variable from the current session.

Alternatively, you can start a new PowerShell session to load the updated environment.

Conclusion

In this tutorial, I have explained different methods to remove environment variables using PowerShell. The Remove-Item cmdlet and setting the variable value to an empty string are the safest and easiest ways to delete environment variables; I also recommend using these two methods. Directly modifying the registry is also possible but riskier. Remember to refresh your PowerShell environment after making changes to reflect the updates in your current session.

I hope you now have a complete understanding of removing the environment variable in PowerShell. Feel free to leave a comment below if you still have questions.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.