Initialize-Winget.ps1

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID df32c5bb-3f4a-46d3-9304-96addbf693f4
 
.AUTHOR Jonathan Pitre
 
.TAGS PowerShell Module Automation
 
.RELEASENOTES
 
1.0 - 2025-05-28
Initial release.
 
#>


<#
.SYNOPSIS
    Initializes and verifies WinGet installation, attempting repairs if necessary.
 
.DESCRIPTION
    This script ensures WinGet is properly installed and functional. It will:
    1. Verify WinGet can find packages using the 'Microsoft.WinGet.Client' module.
    2. Attempt to repair WinGet if package search fails.
    3. Install WinGet and its dependencies if repair fails.
    4. Locate and verify the winget.exe path.
 
    The script also ensures that necessary helper modules ('Microsoft.WinGet.Client', 'PSAppDeployToolkit.WinGet') are installed and imported.
 
.PARAMETER WinGetId
    The WinGet package ID to verify functionality (e.g., "Microsoft.AppInstaller"). This is used to test if WinGet is working properly by attempting to find this package.
 
.EXAMPLE
    Initialize-WinGet -WinGetId "Microsoft.AppInstaller"
    # Initializes WinGet and verifies it can find the Microsoft App Installer package.
 
.OUTPUTS
    The found WinGet package information if successful.
 
.NOTES
    This function requires and will attempt to install/update the following PowerShell modules from the PowerShell Gallery:
    - 'Microsoft.WinGet.Client': Provides cmdlets to interact with the WinGet service, allowing for searching, installing, and managing packages.
    - 'PSAppDeployToolkit.WinGet': A module likely used to integrate WinGet operations within the PowerShell App Deployment Toolkit framework or provide supplementary WinGet functionalities.
 
    Administrative privileges are generally required for installing/updating these modules and for WinGet repair/installation operations.
    An active internet connection is needed to download modules from the PowerShell Gallery and for WinGet to function.
#>


[CmdletBinding()]
param (
    [Parameter(Mandatory = $false)]
    [string]$WinGetId = 'Microsoft.AppInstaller',
    [Parameter(Mandatory = $false)]
    [string]$LogPath = "$env:ProgramData\Microsoft\IntuneManagementExtension\Logs",
    [Parameter(Mandatory = $false)]
    [string]$LogFile = 'WinGet_Initialization.log'
)

# Create log directory if it doesn't exist
if (-not (Test-Path -Path $LogPath)) {
    New-Item -Path $LogPath -ItemType Directory -Force | Out-Null
}

# Start transcript
Start-Transcript -Path (Join-Path -Path $LogPath -ChildPath $LogFile) -Force -Append

try {
    Write-Host 'Starting WinGet initialization process...' -ForegroundColor Cyan
    Write-Host "Log file location: $LogFile" -ForegroundColor Cyan

    # Install required modules
    $requiredModules = @('Microsoft.WinGet.Client', 'PSAppDeployToolkit.WinGet')
    foreach ($module in $requiredModules) {
        if (-not (Get-Module -ListAvailable -Name $module)) {
            Write-Host "Installing required module: $module..." -ForegroundColor Cyan
            $null = Install-Module -Name $module -Force -Scope AllUsers -AllowClobber -Repository PSGallery -ErrorAction SilentlyContinue
        } else {
            Write-Host "Updating module: $module" -ForegroundColor Cyan
            Update-Module -Name $module -ErrorAction SilentlyContinue
        }
        Import-Module -Name $module -Force
    }

    function Get-WinGetPath {
        # Get the latest winget.exe path
        try {
            $WinGet = Get-ChildItem -Path "$env:ProgramW6432\WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe\winget.exe" -ErrorAction Stop |
                Sort-Object LastWriteTime -Descending |
                Select-Object -First 1 -ExpandProperty FullName

            if (-not $WinGet) {
                Write-Warning 'Failed to locate winget.exe.'
            }
        } catch {
            Write-Warning "Failed to locate winget.exe: $_"
        }
        return $WinGet
    }

    try {
        # Verify if WinGet path can be found
        $WinGet = Get-WinGetPath
        # Try to find WinGet package
        $WinGetSearchResult = Find-ADTWinGetPackage -Id $AppWinGetId -Count 1 -Source winget

    } catch {
        Write-Warning "Failed to execute Winget command: $_"
        Write-Warning 'Attempting to repair WinGet...'

        try {
            # Verify if WinGet path can be found
            $WinGet = Get-WinGetPath
            # Try to repair WinGet first
            Repair-WinGetPackageManager -AllUsers -Force -Latest

            # Verify if repair worked by trying to find package again
            $WinGetSearchResult
        } catch {
            Write-Warning "Failed to repair WinGet: $_"
            Write-Warning 'Attempting to install WinGet and dependencies...'

            try {
                # Install WinGet and dependencies
                $WingetInstallScriptDirectory = Join-Path -Path $env:ProgramW6432 -ChildPath 'WindowsPowerShell\Scripts'

                # Ensure the target directory exists. New-Item -Force creates parent directories if needed.
                if (-not (Test-Path -Path $WingetInstallScriptDirectory)) {
                    New-Item -ItemType Directory -Path $WingetInstallScriptDirectory -Force -ErrorAction SilentlyContinue | Out-Null
                }

                # Save the script to the determined target directory.
                # Using Save-Script instead of Install-Script to precisely control the installation path to ensure it's in the 64-bit Program Files.
                Save-Script -Name winget-install -Path $WingetInstallScriptDirectory -Force
                $WingetInstallScript = Join-Path -Path $WingetInstallScriptDirectory -ChildPath 'winget-install.ps1'

                # Run the installation script with system context
                Start-Process -FilePath "$($env:WINDIR)\SysNative\WindowsPowerShell\v1.0\powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$WingetInstallScript`" -Force -ForceClose" -Wait -NoNewWindow

                # Verify if installation worked
                $WinGet = Get-WinGetPath
                # Try to find WinGet package
                $WinGetSearchResult
            } catch {
                Write-Warning "Failed to install WinGet: $_"
                Write-Warning 'Attempting to install WinGet and dependencies with alternate method...'

                try {
                    # Install WinGet and dependencies with alternate method
                    Start-Process -FilePath 'powershell.exe' -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$WingetInstallScript`" -Force -ForceClose -AlternateInstallMethod" -Wait -NoNewWindow

                    # Verify if installation worked
                    $WinGet = Get-WinGetPath
                    # Try to find WinGet package
                    $WinGetSearchResult
                } catch {
                    throw "Failed to install WinGet: $_"
                }
            }
        }
    }

    $WinGetVersion = (& $WinGet --version).Trim('v')
    Write-Host "WinGet version: $WinGetVersion" -ForegroundColor Green
    Write-Host "Winget.exe is installed at: $WinGet" -ForegroundColor Green
    Write-Host "Found WinGet package: $WinGetId" -ForegroundColor Green
} catch {
    throw "An error occurred during WinGet initialization: $_"
} finally {
    # Stop transcript
    Stop-Transcript
}