# Script to install winget. # Technolog Networks, 2025-10-18 # iRaven <# .SYNOPSIS Updates Nextcloud Client .PARAMETER MDT Set if in use with MDT for logging #> Start-Transcript -Path "c:\irnh\Install-WinGet.log" -Append $PSModulePathAU = $env:PSModulePath.split(';')[1] # All Users PSModule path if ($MDT){ $MDTPath = $Global:TSEnv.Value("DeployRoot") } # See if the shit we need exists. Function Check-WinGet-Install { try { winget --version Write-Host "WinGet already exists!" return $true } catch { Write-Host "WinGet does not exist!" return $false } } Function ImportPSWinGet { Write-Host -ForegroundColor Yellow "Importing Winget PS module..." try { if (!(Test-Path -Path $PSModulePathAU\Microsoft.WinGet.Client -PathType Container)){ # Get from PSGallery/NuGet Write-Host -ForegroundColor Yellow "Installing Microsoft.WinGet.Client from PSGallery" if (!(Get-PackageProvider -ListAvailable -Name 'NuGet' -ErrorAction Ignore)) { Write-Host -ForegroundColor Yellow 'Installing NuGet package provider...' Install-PackageProvider -Name 'NuGet' -Force Install-Module -Name Microsoft.WinGet.Client -Scope AllUsers -Force } else { Install-Module -Name Microsoft.WinGet.Client -Scope AllUsers -Force } } Import-Module Microsoft.WinGet.Client -Force Write-Host -ForegroundColor Green "Microsoft.WinGet.Client module imported!" Write-LogEntry -Value "Microsoft.WinGet.Client module imported!" -Severity 1 -Component "ImportPSWindowsUpdate" -FileName $ScriptLog return $true } catch { Write-LogEntry -Value "Failed to import PSWindowsUpdate!" -Severity 3 -Component "ImportPSWindowsUpdate" -FileName $ScriptLog Write-Host -ForegroundColor Red "Failed to import PSWindowsUpdate!" return $false } } Function Install-WinGet { if (ImportPSWinGet){ try { repair-wingetpackagemanager } catch { Write-Host -ForegroundColor Red "Couldn't repair/install WinGet" } } } Function Write-LogEntry { #Write data to a CMTrace compatible log file. (Credit to SCConfigMgr - https://www.scconfigmgr.com/) param( [parameter(Mandatory = $true, HelpMessage = "Value added to the log file.")] [ValidateNotNullOrEmpty()] [string]$Value, [parameter(Mandatory = $true, HelpMessage = "Severity for the log entry. 1 for Informational, 2 for Warning and 3 for Error.")] [ValidateNotNullOrEmpty()] [ValidateSet("1", "2", "3")] [string]$Severity, [parameter(Mandatory = $true, HelpMessage = "Component of the log file.")] [ValidateNotNullOrEmpty()] [string]$Component, [parameter(Mandatory = $false, HelpMessage = "Name of the log file that the entry will written to.")] [ValidateNotNullOrEmpty()] [string]$FileName ) #Determine log file location $LogFilePath = $FileName #Construct time stamp for log entry if(-not(Test-Path -Path 'variable:global:TimezoneBias')) { [string]$global:TimezoneBias = [System.TimeZoneInfo]::Local.GetUtcOffset((Get-Date)).TotalMinutes if($TimezoneBias -match "^-") { $TimezoneBias = $TimezoneBias.Replace('-', '+') } else { $TimezoneBias = '-' + $TimezoneBias } } $Time = -join @((Get-Date -Format "HH:mm:ss.fff"), $TimezoneBias) #Construct date for log entry $Date = (Get-Date -Format "MM-dd-yyyy") #Construct context for log entry $Context = $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name) #Construct final log entry $LogText = "" #Add value to log file try { Out-File -InputObject $LogText -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop } catch [System.Exception] { Write-Warning -Message "Unable to append log entry to $FileName file. Error message at line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)" } } # Main script if ($MDT){ $ScriptLog = "$MDTPath\_Logs\$env:ComputerName-WinGet.log" } else { $ScriptLog = "$env:SystemDrive\irnh\Install-WinGet.log" } if (!(Check-WinGet-Install)) { Install-WinGet } Stop-Transcript