Windows deserves a kick

winget is broken again.

3 min read.

… because of course it is!

Ah well, winget is a great idea with a decent implementation, suffering from an unstable platform it’s built on. I’ve now seen it break down seemingly randomly 3 times – once it was one of the many reasons I had to reinstall Windows completely.

But as usual – let’s take a step back. How and why did I get started on this via dolorosa?

Background

A while back I was going to use winget to update my apps to their latest versions, as I’ve done before. For that, I have a script like below:

winget upgrade --all -u --disable-interactivity --accept-source-agreements --accept-package-agreements --force

But to my surprise, I was met with an error:

winget: The term ‘winget’ is not recognized as a name of cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Or in a picture below:

What’s wrong now?

Reason

Ah, winget – the package manager for Windows, that keeps breaking down and requiring other tools to fix it. It’s a shame it was built in such brittle way. It showed such promise, but it just had to be another WinUI…

Anyway.

There are a lot of reasons why it would break, but usually it seems to be Windows Update messing with your system. The other option is the app missing from your Path variable.


The command below will register/install winget for your current context and should add it to your Path variable too:

Add-AppxPackage -RegisterByFamilyName -MainPackage Microsoft.DesktopAppInstaller_8wekyb3d8bbwe

But if this doesn’t help, you need to start actually fixing the app.


Now, as luck would have it, Microsoft knows winget is prone to breaking, and they have a PowerShell commandlet to fix it. Very forward-looking of them.

install-module microsoft.winget.client -Force -AllowClobber 
import-module microsoft.winget.client

And then we can:

Repair-WingetPackageManager -Force -Latest -Verbose

Still running into the same issue? You might want to try this:

DISM.exe /Online /Cleanup-image /Restorehealth
sfc /scannow

And restart your machine. It’ll take a while, but has fixed winget for me… Once.


Fix your WindowsApps permissions

Still doesn’t work? Well, Windows Update might have messed with your permissions (and Repair-WingetPackageManager might not be able to fix it).

Try accessing the following folder:

C:\Program Files\WindowsApps

If you can’t access it, you need to grant yourself access to it. You can do that with PowerShell:

takeown /f "C:\Program Files\WindowsApps" /r

Or by using File Explorer:

  1. Right-click the WindowsApps folder.
  2. Select Properties.
  3. Go to the Security tab and click Advanced.
  4. Under Owner, click Change.
  5. Click Advanced again, then Find Now.
  6. Select your user name from the list, click OK, then OK again.
  7. Apply the changes and try accessing the folder again.

But what if after accessing the folder (and the app-specific folders below it), all of your Windows App executables are now 0kb in size?

Well, the obvious solution would be to use winget to reinstall all of the apps. While all of the other apps can be broken, Repair-WingetPackageManager should’ve fixed winget itself. However, even then winget might not be in your path – so what you can do is to change directory (command cd) to “C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_[version-string]_[architecture-string]__8wekyb3d8bbwe”, and run winget export to export the currently (supposedly) installed apps, read the JSON, and reinstall every single app.

Something like this:

$jsonFilePath = "C:\temp\winget_export.json"

winget export -o $jsonFilePath

# Read the JSON file into a PowerShell object
$jsonContent = Get-Content -Path $jsonFilePath -Raw | ConvertFrom-Json

# Loop through each package and install it using winget
foreach ($source in $jsonContent.Sources) {
    foreach ($package in $source.Packages) {
        $packageId = $package.PackageIdentifier
        winget install --id $packageId --silent --accept-package-agreements --accept-source-agreements --uninstall-previous --force
    }
}

This produces a lot of unnecessary output as winget tries to export also apps that aren’t available in your winget repositories, and will take a looooong time, but in an optimal world, it’ll fix your packages.

And with all that done, usually you’re good. And if you’re not, it’s always a great option and fun pastime activity to reinstall your Windows machine (you might want to start by “resetting” it – there’s always the chance that actually does something!)

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments