winutil icon indicating copy to clipboard operation
winutil copied to clipboard

Fix for Windows 11 23H2 Winget

Open SkylerWallace opened this issue 2 years ago • 3 comments

Windows 11 23H2 ships with winget partially installed. The result is that many winget commands work however winget install is non-functional.

The solution until now has been to launch the MS Store and update App Installer prior to running winget or WinUtil. Chris has also commented on this solution here: https://github.com/ChrisTitusTech/winutil/issues/1178

While the current solution is workable, people unfamiliar with the issue would be left confused.

I have been able to pinpoint several markers that would indicate if this issue is present. The simplest and quickest way I have tested is checking the winget version: all Windows 11 23H2 images I have tested (stock and OEM) have Winget version v1.2.10691.

This is a great indicator for these reasons:

  1. winget --version is still usable even with the issue present
  2. winget --version is quick to run
  3. If we use the winget version to indicate if the issue is present, we can also simultaneously resolve issues with people using older versions of winget
  4. Verified this version number is present on stock & new-in-box OEM laptops on 23H2

I have my own GitHub repo that shares many features with WinUtil. In one of my scripts I have the logic to resolve this issue: https://github.com/SkylerWallace/ATOM/blob/main/ATOM/Dependencies/Neutron/Functions/Install-Programs.ps1

Here is the code snippet:

# Install Winget if not detected
$wingetExists = Get-Command -Name winget -ErrorAction SilentlyContinue
if ($wingetExists) {
	$wingetVersion = [System.Version]::Parse((winget --version).Trim('v'))
	$minimumWingetVersion = [System.Version]::new(1,2,10691) # Win 11 23H2 comes with bad winget v1.2.10691
	$wingetOutdated = $wingetVersion -le $minimumWingetVersion
	
	Write-Host "Winget v$wingetVersion"
}

if (!$wingetExists -or $wingetOutdated) {
	if (!$wingetExists) {
		Write-Host "Winget not detected"
	} else {
		Write-Host "- Winget out-dated"
	}
	
	try {
		Write-Host "- Attempting first install method..."
		
		$wingetURL = "https://github.com/microsoft/winget-cli/releases/latest/download/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
		$wingetFileName = Split-Path $wingetURL -Leaf
		$wingetInstallerPath = Join-Path $env:TEMP $wingetFileName
		
		Invoke-WebRequest -Uri $wingetURL -OutFile $wingetInstallerPath
		Add-AppxPackage -Path $wingetInstallerPath
	} catch {
		Write-Host "- Attempting second install method..."
		
		Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
		Install-Script -Name winget-install -Force
		$wingetArgument = "-ExecutionPolicy Bypass winget-install.ps1"
		Start-Process powershell -ArgumentList $wingetArgument -Wait
	}
	
	$wingetExists = Get-Command -Name winget -ErrorAction SilentlyContinue
	if ($wingetExists) {
		$wingetVersion = [System.Version]::Parse((winget --version).Trim('v'))
		Write-Host "- Installed Winget v$wingetVersion"
	} else {
		Write-Host "- Failed to install Winget"
	}
}

This snippet uses the following URL to download winget: https://github.com/microsoft/winget-cli/releases/latest/download/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle

Using releases/latest/download will always download the latest tagged release so long as Microsoft doesn't rename Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle

Alternatively you can use this URL as it's an official Microsoft URL and seems to also download the latest winget release consistently: https://aka.ms/getwinget

I have tested this solution on several devices and VMs, it has always worked.

I have checked through winget.ps1 to see how this solution could be implemented in your code but ultimately decided I may implement this in a way that isn't entirely cohesive with your vision, as a result I have not attached any code to commit. I hope you can somehow implement this solution into your project!

SkylerWallace avatar Feb 03 '24 18:02 SkylerWallace

Love this snippet. I'd probably add a third catch to choco install winget as the install script sometimes fails as well.

ChrisTitusTech avatar Feb 03 '24 18:02 ChrisTitusTech

Appreciate the idea, modified it for the project in commit feafae6deda78c3956ccf8e129a4354622ad73ac

ChrisTitusTech avatar Feb 03 '24 19:02 ChrisTitusTech

I think winget is just broken.(See image below)

image It won't install a package by microsoft.

TheAnonymousXII avatar Feb 06 '24 22:02 TheAnonymousXII