Use Winget to update all packages currently installed on a computer

# Get the Windows version
$osInfo = Get-WmiObject -Class Win32_OperatingSystem

# Parse the version number and product type
$version = [Version] $osInfo.Version
$productType = $osInfo.ProductType

# Check the Windows version and product type
if (($productType -eq 1 -and $version -ge [Version] "10.0.17763") -or 
    ($productType -eq 3 -and $version -ge [Version] "10.0.20348")) {

    # Change directory to WindowsApps
    Set-Location "C:\Program Files\WindowsApps\"

    # Set the installer directory
    $installer = "Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe"
    if ( (Get-ChildItem $installer).Count -gt 1 ) {
        $appx = (Get-ChildItem $installer)[1]
    } else { 
        $appx = Get-ChildItem $installer 
    }
    # Change to the selected directory
    Set-Location $appx

    # Run the winget upgrade
    .\winget.exe upgrade --all -h --accept-package-agreements --accept-source-agreements

} else {
    Write-Output "The system does not meet the minimum requirements. This script requires Windows 10 1809 or later, any version of Windows 11, or Windows Server 2022."
}

Script data

Language - PowerShell

Run as - System / Root User

Script timeout duration - 15 Mins

Read me

Description The script changes the directory to the WindowsApps folder, finds the latest Microsoft Desktop App Installer, then uses the winget upgrade command to update all installed applications. Prerequisites You need to have Windows Package Manager (winget) installed on your system. The script needs to be run with administrator privileges to access the WindowsApps directory and execute the upgrade command. The script will automatically accept package and source agreements for the update process. Script Breakdown Here is a step by step breakdown of what the script does: cd "C:\Program Files\WindowsApps\" - Changes the directory to the WindowsApps folder, where winget is located. $installer = "Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe" - Sets the name of the winget installer folder. The * is a wildcard that matches any characters, allowing for different versions of the installer. if ( (get-childitem $installer).count -gt 1 ) { $appx = (get-childitem $installer)[1] } else { $appx = get-childitem $installer } - Checks the number of matches for the installer. If there is more than one match, it selects the second match. Otherwise, it selects the only match. cd $appx - Changes the directory to the selected installer folder. .\winget.exe upgrade --all -h --accept-package-agreements --accept-source-agreements - Executes the winget upgrade command to update all installed applications, automatically accepting package and source agreements. Caution The script will automatically accept all package and source agreements. Ensure you understand the implications of this before running the script.