Public/Remove-EnvironmentVariable.ps1
|
using namespace System function Remove-EnvironmentVariable { <# .SYNOPSIS Removes an environment variable from the specified scope. .DESCRIPTION Deletes an environment variable based on its key and optional value from the specified scope. If the value is not provided, the function removes the variable based solely on the key. .PARAMETER Key The name of the environment variable to remove. .PARAMETER Value The value of the environment variable to remove. This parameter is optional and can be used to remove a specific value from an environment variable. .PARAMETER Scope Specifies the scope of the environment variable to remove. The default is Process. The terminal session requires a restart if the scope is not set to Process for the changes to take effect. On Linux and macOS, only the Process scope is supported. .INPUTS None. You can't pipe objects to Remove-EnvironmentVariable. .OUTPUTS None. This function does not produce any output. .EXAMPLE PS> Remove-EnvironmentVariable -Key PROFILE_ENABLE_BRANCH_USERNAME Removes the PROFILE_ENABLE_BRANCH_USERNAME environment variable from the Process scope. .EXAMPLE PS> Remove-EnvironmentVariable -Key PATH -Value "C:\Program Files\bin" -Scope User Removes "C:\Program Files\bin" from PATH. .NOTES On Linux and macOS, .NET only supports the Process scope for environment variables. The User and Machine scopes are ignored by the runtime, so on those platforms this Cmdlet emits a warning and performs no action when a non-Process scope is requested. .LINK https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables #> [OutputType([void])] [CmdletBinding(SupportsShouldProcess, ConfirmImpact = "High")] param( [Parameter(Position = 0, Mandatory)] [string] $Key, [Parameter(Position = 1)] [string] $Value, [EnvironmentVariableTarget] $Scope = [EnvironmentVariableTarget]::Process ) begin { $Token = $IsWindows ? ";" : ":" } process { if (!$IsWindows -and $Scope -ne [EnvironmentVariableTarget]::Process) { Write-Warning "On Linux and macOS, only the Process scope is supported; the '$Scope' scope has no effect." return } $Title = "Remove `"${Value}`" from `"${Key}`"" $Description = "Are you sure that you want to remove `"${Value}`" from the environment variable `"${Key}`"?" $RemoveValue = $([Environment]::GetEnvironmentVariable($Key, $Scope) -Split $Token | Where-Object { $_ -ne $Value }) -Join $Token if (!$PSBoundParameters.ContainsKey("Value")) { $Title = "Remove all values in `"${Key}`"" $Description = "Are you sure that you want to remove the environment variable `"${Key}`"?" $RemoveValue = $null } if ($PSCmdlet.ShouldProcess($null, $Description, $Title)) { [Environment]::SetEnvironmentVariable($Key, $RemoveValue, $Scope) } } } |