Skip to content

Instantly share code, notes, and snippets.

@Patrikios
Last active April 29, 2023 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Patrikios/29350760e2a54fe79cfddd120f4d2196 to your computer and use it in GitHub Desktop.
Save Patrikios/29350760e2a54fe79cfddd120f4d2196 to your computer and use it in GitHub Desktop.
poweshel alias function to delete and add new value to path
Set-Alias delp Remove-EnvironmentPath
function Remove-EnvironmentPath {
$toRemove = ($args -join " ").TrimEnd("\")
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User")
$changedUserPath = $userPath.Split(";") |
ForEach-Object { $_.TrimEnd("\").Trim() } |
Where-Object { $_ -ne $toRemove }
$changedUserPath = $changedUserPath -Join ";"
if ($userPath -ne $changedUserPath) {
[Environment]::SetEnvironmentVariable("PATH", "$changedUserPath", "User")
$env:PATH = [Environment]::GetEnvironmentVariable("PATH", "Machine")
$env:PATH += ";$changedUserPath"
return
}
echo "Not removed"
$machinePath = [Environment]::GetEnvironmentVariable("PATH", "Machine")
$isInMachine = $machinePath.split(";") | Where-Object { $_.Trim("\") -eq $toRemove }
if ($isInMachine) {
echo "Is present in Machine scope"
}
}
# remove from path
# delp C:\xyz
Set-Alias ap Append-EnvironmentPath
function Append-EnvironmentPath {
$toAppend = ($args -join " ").TrimEnd("\")
if (-not (Test-Path $toAppend)) {
Write-Host "Path doesn't exist..."
Write-Host $toAppend
return
}
$env:PATH = $env:PATH + ";$toAppend"
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User")
[Environment]::SetEnvironmentVariable("PATH", "$userPath;$toAppend", "User")
}
# add to path
# ap C:\xyz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment