Skip to content

Instantly share code, notes, and snippets.

@deas
Created February 16, 2026 06:39
Show Gist options
  • Select an option

  • Save deas/ad084689a7eb71eade0f4012e286f5be to your computer and use it in GitHub Desktop.

Select an option

Save deas/ad084689a7eb71eade0f4012e286f5be to your computer and use it in GitHub Desktop.
# Append or remove a path entry to default powershell profile
# Update-Path -TargetFolder "C:\Program Files\Git\usr\bin" -Action Add
# Update-Path -TargetFolder "C:\Program Files\Git\usr\bin" -Action Remove
function Update-Path {
param (
[Parameter(Mandatory=$true)]
[string]$TargetFolder,
[ValidateSet("Add", "Remove")]
[string]$Action = "Add"
)
# 1. Ensure the Profile exists
if (!(Test-Path $PROFILE)) {
Write-Host "Creating new PowerShell profile at $PROFILE..." -ForegroundColor Cyan
$null = New-Item -Path $PROFILE -ItemType File -Force
}
# The exact string we'll look for/add in the profile
$profileLine = "`$env:Path += ';$TargetFolder'"
# 2. Handle the persistent profile update
$profileContent = Get-Content $PROFILE
if ($Action -eq "Add") {
if ($profileContent -contains $profileLine) {
Write-Host "Path already exists in profile." -ForegroundColor Yellow
} else {
Add-Content -Path $PROFILE -Value "`n$profileLine"
Write-Host "Added to profile." -ForegroundColor Green
}
# Update current session immediately
if ($env:Path -notlike "*$TargetFolder*") {
$env:Path += ";$TargetFolder"
}
}
else { # Action is Remove
$newContent = $profileContent | Where-Object { $_ -ne $profileLine }
$newContent | Set-Content $PROFILE
# Note: Removing from the *current* session's $env:Path is messy
# because of semicolons, so it's best to just restart the shell.
Write-Host "Removed from profile. Restart PowerShell for full effect." -ForegroundColor Yellow
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment