Remove-EnvPath.ps1
19 Sep 2025Description
Purpose
Removes a path from the environment variable ‘Path’ for the specified container.
Detailed Description
This function removes a path from the environment variable ‘Path’ for the specified container. The container can be ‘Machine’, ‘User’, or ‘Session’. If the container is ‘Session’, the function only removes the path from the current session’s environment variable ‘Path’. If the container is ‘Machine’ or ‘User’, the function also removes the path from the persisted environment variable ‘Path’ for the specified container.
Usage
Example 1
Remove-EnvPath -Path 'C:\temp' -Container 'User'
This example removes the path ‘C:\temp’ from the environment variable ‘Path’ for the user container.
Notes
No additional notes.
Script
function Remove-EnvPath {
<#
.SYNOPSIS
Removes a path from the environment variable 'Path' for the specified container.
.DESCRIPTION
This function removes a path from the environment variable 'Path' for the specified container. The container can be 'Machine', 'User', or 'Session'. If the container is 'Session', the function only removes the path from the current session's environment variable 'Path'. If the container is 'Machine' or 'User', the function also removes the path from the persisted environment variable 'Path' for the specified container.
.PARAMETER Path
The path to remove from the environment variable 'Path'.
.PARAMETER Container
The container for the environment variable 'Path'. The default value is 'Session'.
Valid values are:
- Machine
- User
- Session
.EXAMPLE
Remove-EnvPath -Path 'C:\temp' -Container 'User'
This example removes the path 'C:\temp' from the environment variable 'Path' for the user container.
#>
[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 ';'
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.