Last active
April 29, 2023 11:51
-
-
Save Patrikios/29350760e2a54fe79cfddd120f4d2196 to your computer and use it in GitHub Desktop.
poweshel alias function to delete and add new value to path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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