Remove-EnvironmentVariable

Removes an environment variable.

Syntax

Remove-EnvironmentVariable -Name <String> [-ForComputer] [-ForUser] [-ForProcess] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
Remove-EnvironmentVariable -Name <String> -ForUser -Credential <PSCredential> [-WhatIf] [-Confirm] [<CommonParameters>]

Description

Uses the .NET Environment class to remove an environment variable from the Process, User, or Computer scopes.

Changes to environment variables in the User and Machine scope are not picked up by running processes. Any running processes that use this environment variable should be restarted.

Normally, you have to restart your PowerShell session/process to no longer see the variable in the env: drive. Use the -Force switch to also remove the variable from the env: drive. This functionality was added in Carbon 2.3.0.

Beginning with Carbon 2.3.0, you can set an environment variable for a specific user by specifying the -ForUser switch and passing the user's credentials with the -Credential parameter. This runs a separate PowerShell process as that user to remove the variable.

Beginning in Carbon 2.3.0, you can specify multiple scopes from which to remove an environment variable. In previous versions, you could only remove from one scope.

Related Commands

Parameters

Name Type Description Required? Pipeline Input Default Value
Name String

The environment variable to remove.

true false
ForComputer SwitchParameter

Removes the environment variable for the current computer.

false false False
ForUser SwitchParameter

Removes the environment variable for the current user.

false false False
ForProcess SwitchParameter

Removes the environment variable for the current process.

false false False
Force SwitchParameter

Remove the variable from the current PowerShell session's env: drive, too. Normally, you have to restart your session to no longer see the variable in the env: drive.

This parameter was added in Carbon 2.3.0.

false false False
Credential PSCredential

Remove an environment variable for a specific user.

true false
WhatIf SwitchParameter false false
Confirm SwitchParameter false false
CommonParameters This cmdlet supports common parameters. For more information type
Get-Help about_CommonParameters.

EXAMPLE 1

Remove-EnvironmentVariable -Name 'MyEnvironmentVariable' -ForProcess

Removes the MyEnvironmentVariable from the process scope.

EXAMPLE 2

Remove-EnvironmentVariable -Name 'SomeUsersVariable' -ForUser -Credential $credential

Demonstrates that you can remove another user's user-level environment variable by passing its credentials to the Credential parameter. This runs a separate PowerShell process as that user to remove the variable.