Param( #Defines Parameters that will be used within this script. [Parameter(Mandatory=$true)] #This is an attribute applied to the parameter. It specifies that a parameter is required. [string]$WingetPackageID #[String] is the type of parameter. $WingetPackageID is the name of the parameter. ) #Parameters will be defined in the install/uninstall arguements of the Intune Application when adding the app. For example: #powershell.exe -ExecutionPolicy Bypass -File .\WingetInstall.ps1 -WingetPackageID "Valve.Steam" #powershell.exe -ExecutionPolicy Bypass -File .\WingetUninstall.ps1 -WingetPackageID "Valve.Steam" #This begins recording a transcript of this script and saves it to the below path Start-Transcript -Path "C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\$($WingetPackageID)_Install.log" -Append #Writes in the console the below message Write-Host "Starting Transcript for Winget Installation" -ForegroundColor Green try { #Attempts to run the below code #Writes in the console the below message Write-Host "Attempting to install $($WingetPackageID) via Winget" #Resolves the path of Winget.exe - Looks for the full path of Winget with the given path pattern. The * in the pattern acts as a wildcard, allowing for any folder name to match. $ResolveWingetPath = Resolve-Path 'C:\Program Files\WindowsApps\*\winget.exe' if ($ResolveWingetPath){ #Checks if ResolveWingetPath has data within it #Assuming that the last path of the array represents the lastest or primary installation of Winget $WingetPath = $ResolveWingetPath[-1].Path #Writes in the console what the value of WingetPath is Write-Host $WingetPath } else { #If unable to find Winget Path. It will present this in the console. Throw "Unable to find Winget" } #'&' invokes/executes Winget and allows us to use winget linked arguements such as 'install' #Attempts to install Winget application & $WingetPath install --exact $WingetPackageID --silent --accept-source-agreements --accept-package-agreements --disable-interactivity #--accept-source-agreements --accept-package-agreements - This is needed as devices that have never ran Winget performed will be prompted to accept terms and conditions. This bypasses this. #--disable-interactivity - Attempts to disable user interactivity } Catch { #Will run the below code if the above try fails Throw "Failed to install Package. [$($_.Exception.Message)] See: %LOCALAPPDATA%\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\DiagOutputDir" #This is the path for Winget logs. There will only be logs here if Winget attempted to install/uninstall. } Stop-Transcript