Skip to content

Instantly share code, notes, and snippets.

@hbread00
Last active May 26, 2025 21:30
Show Gist options
  • Save hbread00/6afdde13dc0d9e9876b7bf8c81468517 to your computer and use it in GitHub Desktop.
Save hbread00/6afdde13dc0d9e9876b7bf8c81468517 to your computer and use it in GitHub Desktop.
PowerShell script to add winget package path to environment variables PATH, only for portable packages
# Get user's Winget packages directory
$userProfile = [System.Environment]::GetFolderPath('UserProfile')
$wingetDir = Join-Path $userProfile "AppData\Local\Microsoft\WinGet\Packages"
$pathsToAdd = @()
Get-ChildItem -Path $wingetDir -Directory | ForEach-Object {
$packageDir = $_.FullName
# Find .exe
$exeFile = Get-ChildItem -Path $packageDir -Recurse -Filter *.exe -File -Depth 0 -ErrorAction SilentlyContinue
if ($exeFile) {
$exeDir = Split-Path $exeFile[0].FullName
if (-not ($pathsToAdd -contains $exeDir)) {
$pathsToAdd += $exeDir
}
return
}
# No .exe directly, try find bin
$binDir = Get-ChildItem -Path $packageDir -Directory -Recurse `
| Where-Object { $_.Name -eq "bin" } | Select-Object -First 1
if ($binDir) {
$exeInBin = Get-ChildItem -Path $binDir.FullName -Filter *.exe -File
if ($exeInBin) {
$binDirPath = $binDir.FullName
if (-not ($pathsToAdd -contains $binDirPath)) {
$pathsToAdd += $binDirPath
}
return
}
}
# No bin, try find other directories having .exe
$firstExe = Get-ChildItem -Path $packageDir -Recurse -Filter *.exe -File | Select-Object -First 1
if ($firstExe) {
$exeDir = Split-Path $firstExe.FullName
if (-not ($pathsToAdd -contains $exeDir)) {
$pathsToAdd += $exeDir
}
return
}
Write-Host "No .exe file found in $packageName, skipping..."
}
foreach ($exePath in $pathsToAdd) {
# Get existing PATH
$currentPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::User)
$normalizedPath = $currentPath -replace '/', '\'
$existingPath = $normalizedPath -split ';'
if ($existingPath -contains $exePath) {
Write-Host "$exePath" -ForegroundColor Red
Write-Host "Already exists in PATH, skipped."
continue
}
Write-Host "$exePath" -ForegroundColor Green
Write-Host "Add to PATH? (y/n): " -NoNewline
$answer = Read-Host
if ($answer -eq 'y') {
$currentUserPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::User)
$newUserPath = $currentUserPath + ";$exePath"
[System.Environment]::SetEnvironmentVariable('PATH', $newUserPath, [System.EnvironmentVariableTarget]::User)
Write-Host "Added:$exePath" -ForegroundColor Blue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment