This script is designed to download and install the Winget-WindowsPackageManager.
It logs the entire process in a transcript file for later review and debugging. This will be recorded in C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\ named ‘Winget-WindowsPackageManager_Install.log’
#Defines Winget as a variable with the string "Winget-WindowsPackageManager"
$Winget = "Winget-WindowsPackageManager"
#Begins a transcript that will be recorded in C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\ named 'Winget-WindowsPackageManager_Install.log
Start-Transcript -Path "C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\$($Winget)_Install.log" -Append
#Displays the below to the console:
Write-Host "Starting Transcript for Winget-WindowsPackageManager Installation"
#Assigns the path of the user's download folder to the variable: $download folder
$downloadFolder = ('C:\ProgramData\Microsoft\IntuneManagementExtension\Logs')
#This stored the URL: https://aka.ms/getwinget - If you go to this URL, it will install the application package (.msixbundle) for winget
$downloadUrl = "https://aka.ms/getwinget"
#This line assigns the string value "Microsoft.DesktopAppInstaller_8wekyb3d8bbwe_{0}.msixbundle" to the variable $downloadFileName.
$downloadFileName = ("Microsoft.DesktopAppInstaller_8wekyb3d8bbwe_{0}.msixbundle")
#This line assigns the combined value of $downloadFolder and $downloadFileName to the variable $downloadPath.
#Hee '{0}\{1}' -f $downloadFolder, $downloadFileName is a formats the string by inserting the values of $downloadFolder and $downloadFileName into the placeholders {0} and {1} respectively.
$downloadPath = ('{0}\{1}' -f $downloadFolder, $downloadFileName)
#Declares the function 'Invoke-Download'
Function Invoke-Download {
#This line specifies the parameter block for the Invoke-Download function.
param (
#Declares the two parameters $Url and $Path and their data types.
[string]$Url,
[string]$Path
)
#The code inside the try block will be executed, and any exceptions that occur will be caught and handled.
try {
#Displays the below in the console
Write-Host "Invoking Download"
#Displays the below in the console
Write-Host "Downloading $Url TO (->) $Path"
#This line creates a new instance of the System.Net.WebClient class and assigns it to the variable $webClient.
$webClient = New-Object Net.WebClient
#This line uses the $webClient object to download a file from the specified URL ($Url) and saves it to the specified path ($Path).
$webClient.DownloadFile($Url, $Path)
}
#This line starts a catch block. If an exception occurs within the preceding try block, the code inside the catch block will be executed to handle the exception.
catch {
#This line throws an exception with the message "Failed to Invoke Download for Winget Installation".
Throw "Failed to Invoke Download for Winget Installation"
}
}
#This line calls the Invoke-Download function with the arguments $downloadUrl and $downloadPath.
Invoke-Download -Url $downloadUrl -Path $downloadPath
try {
#Displays the below to the console
Write-Host "Attempting to Install Winget-WindowsPackageManager"
#This line installs the Winget Package. The '-SkipLicense' parameter skips the acceptance of the license terms during installation. '-Online' tells powershell to install the application on the current online image of the operating system, rather than on a mounted offline image.
Add-AppxProvisionedPackage -Online -PackagePath $downloadPath -SkipLicense
}
catch {
#Displays the below in the console. 'IsError' makes the text red.
Write-Host -IsError "Failed to install AppxProvisionedPackage: $downloadPath"
#Stops the execution of the current block and transfers control the nearest catch block.
Throw
}
#This block is executed regardless of whether an exception occurs or not. This cleans up the execution of the script. This will stop the transcipt.
finally {
Stop-Transcript
}
Uninstall Script:
$packageName = "Winget-WindowsPackageManager"
$appPackageName = "Microsoft.DesktopAppInstaller"
Start-Transcript -Path "C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\$($packageName)_Uninstall.log" -Append
try {
Write-Host "Running Remove-AppxPackage on $appPackageName"
Get-AppxPackage -Name $appPackageName | Remove-AppxPackage -ErrorAction Stop
}
catch {
Write-Host -IsError "Failed to uninstall AppxPackage: $appPackageName"
Throw
}
Write-Host "Successfully uninstalled AppxPackage: $appPackageName"
Stop-Transcript