Private/Remove-EnvPath.ps1
function Remove-EnvPath { [CmdletBinding(DefaultParameterSetName = 'Default')] param( [Parameter(Mandatory = $true)] [string] $Path, [ValidateSet('Machine', 'User', 'Session')] [string] $Container = 'Session' ) if ($Container -ne 'Session') { $containerMapping = @{ Machine = [EnvironmentVariableTarget]::Machine User = [EnvironmentVariableTarget]::User } $containerType = $containerMapping[$Container] $persistedPaths = [Environment]::GetEnvironmentVariable('Path', $containerType) -split ';' if ($persistedPaths -contains $Path) { $persistedPaths = $persistedPaths | Where-Object { $_ -and $_ -ne $Path } [Environment]::SetEnvironmentVariable('Path', $persistedPaths -join ';', $containerType) } } $envPaths = $env:Path -split ';' if ($envPaths -contains $Path) { $envPaths = $envPaths | Where-Object { $_ -and $_ -ne $Path } $env:Path = $envPaths -join ';' } } |