Automating WinGet Path Fixes and Package Upgrades on Windows with PowerShell

Till LazarevTill Lazarev
4 min read

Managing software on Windows has become incredibly efficient thanks to WinGet, Microsoft’s Windows Package Manager. However, users sometimes face issues where the winget command isn’t recognized due to missing or incorrect path configurations. At Lazarev Cloud, we’ve developed a simple PowerShell script that fixes these path issues automatically and upgrades all your installed software.

In this article, we’ll guide you through using this script, which you can either download or simply paste directly into your Admin PowerShell to get started.


Common WinGet Issues

Sometimes, WinGet isn’t recognized because:

  • The winget.exe path isn’t added to your system’s environment variables.

  • The system registry doesn’t have the correct path for WinGet, making it inaccessible system-wide.

Without proper configuration, you won’t be able to use WinGet commands for software management.


Solution: A PowerShell Script to Fix Everything

Our solution is a PowerShell script that:

  1. Adds the WinGet path to both the system environment variables and registry if it’s missing.

  2. Automatically upgrades all your installed packages using WinGet.

You can either download the script from Lazarev Cloud GitHub or paste the code directly into your PowerShell.


How to Use the Script

Option 1: Paste the Code Directly into Admin PowerShell

If you want to get started quickly without downloading anything, you can paste the code below directly into your Admin PowerShell:

function Add-WinGetPath {
    $WinGetPath = (Get-ChildItem -Path "$env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller*_x64*\winget.exe").DirectoryName
    if (-Not($WinGetPath)) {
        Write-Host "WinGet installation not found. Please ensure it's installed." -ForegroundColor Red
        return
    }

    if (-Not (($Env:Path -split ';') -contains $WinGetPath)) {
        $env:Path += ";" + $WinGetPath + ";"
        Write-Host "WinGet path $WinGetPath added to the current session environment variable." -ForegroundColor Green
    } else {
        Write-Host "WinGet path already exists in the current session environment variable." -ForegroundColor Yellow
    }
}

function Add-WinGetPath-Registry {
    $WinGetPath = (Get-ChildItem -Path "$env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller*_x64*\winget.exe").DirectoryName
    if (-Not($WinGetPath)) {
        Write-Host "WinGet installation not found. Please ensure it's installed." -ForegroundColor Red
        return
    }

    $registryPath = 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment'
    $oldPath = (Get-ItemProperty -Path $registryPath -Name 'Path').Path

    if (-Not (($oldPath -split ';') -contains $WinGetPath))) {
        $newPath = $oldPath + ';' + $WinGetPath
        Set-ItemProperty -Path $registryPath -Name 'Path' -Value $newPath
        [Environment]::SetEnvironmentVariable('Path', $newPath, [EnvironmentVariableTarget]::Machine)
        Write-Host "WinGet path $WinGetPath added to the registry and system-wide environment variable." -ForegroundColor Green
    } else {
        Write-Host "WinGet path already exists in the registry." -ForegroundColor Yellow
    }
}

function Upgrade-WinGetPackages {
    Write-Host "Upgrading all WinGet packages..."
    winget upgrade --all --include-unknown --accept-package-agreements --disable-interactivity
    if ($?) {
        Write-Host "All packages upgraded successfully." -ForegroundColor Green
    } else {
        Write-Host "Failed to upgrade some packages." -ForegroundColor Red
    }
}

function Fix-WinGetPath {
    Write-Host "Checking and fixing WinGet path issues..."
    Add-WinGetPath
    Add-WinGetPath-Registry
    Write-Host "WinGet path check and update completed." -ForegroundColor Cyan
    Upgrade-WinGetPackages
}

# Run the path fix and upgrade functions
Fix-WinGetPath

Instructions:

  1. Run PowerShell as Administrator:

    • Right-click the Start menu.

    • Select Windows PowerShell (Admin) or Windows Terminal (Admin).

  2. Paste the Script:
    Simply copy and paste the code above into your PowerShell window.

  3. Press Enter:
    After pasting, press Enter to execute the script. The script will:

    • Fix any missing WinGet paths in your system environment and registry.

    • Automatically upgrade all installed packages using winget.

Option 2: Download and Run the Script

You can also download the script directly from Lazarev Cloud GitHub:
Download WinGet Fix Script

Once downloaded, follow these steps to run the script:

  1. Open PowerShell as Administrator.

  2. Navigate to the folder where the script is saved:

     cd path\to\script
    
  3. Run the script:

     .\winget.ps1
    

What the Script Does

  1. Adds WinGet Path to Current Session:
    If the path to winget.exe is missing in the current session’s environment variables, the script will add it.

  2. Adds WinGet Path to System Registry:
    It checks the system-wide PATH stored in the registry and adds the WinGet path if it’s not already there.

  3. Upgrades All Installed Packages:
    The script runs the winget upgrade --all command to update all your installed applications.


Benefits of Using This Script

  • Fully Automated: Fixes path issues and upgrades all software in one run.

  • Fast and Reliable: No need for manual path editing or package-by-package upgrades.

  • Customizable: You can modify the script to fit your organization’s or personal needs.

  • Instant Results: Start using WinGet immediately after running the script.


Conclusion

At Lazarev Cloud, we believe in simplifying complex tasks with automation. Whether you’re a power user or managing multiple systems, this WinGet path-fixing and upgrade script will save you time and ensure that your system is always up to date.

Simply paste the code directly into PowerShell or download it from GitHub, and let the script handle the rest!

Get the script from GitHub:
Lazarev Cloud GitHub - WinGet Script


For more helpful scripts and automation tips, follow the Lazarev Cloud Blog or reach out to our support for any questions!

1
Subscribe to my newsletter

Read articles from Till Lazarev directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Till Lazarev
Till Lazarev