Winget Application Install & Uninstall Script

Install Applications Via SYSTEM behaviour:

Install Arguments :

powershell.exe -ExecutionPolicy Bypass -File .\WingetInstallAsSystem.ps1 -WingetPackageID “ENTERWINGETID”


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 

Uninstall Applications Via SYSTEM behaviour:

Uninstall Arguments :

powershell.exe -ExecutionPolicy Bypass -File .\WingetUninstallAsSystem.ps1 -WingetPackageID “ENTERWINGETID”

Param(
    [Parameter(Mandatory=$true)]
    [string]$WingetPackageID
)

Start-Transcript -Path "C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\$($WingetPackageID)_Install.log" -Append
Write-Host "Starting Transcript for Winget Installation" -ForegroundColor Green

try {

    Write-Host "Attempting to install $($WingetPackageID) via Winget"
    $ResolveWingetPath = Resolve-Path 'C:\Program Files\WindowsApps\*\winget.exe'

    if ($ResolveWingetPath){

        $WingetPath = $ResolveWingetPath[-1].Path
        Write-Host $WingetPath
    }

    else {

        Throw "Unable to find Winget"
    }

    & $WingetPath uninstall --exact $WingetPackageID --silent --accept-source-agreements --disable-interactivity       
}

Catch {

    Throw "Failed to install Package. [$($_.Exception.Message)] See: %LOCALAPPDATA%\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\DiagOutputDir"
}

Stop-Transcript

Install Applications Via USER behaviour:

Install Arguments :

powershell.exe -ExecutionPolicy Bypass -File .\WingetInstallAsUser.ps1 -WingetPackageID “ENTERWINGETID”


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:\Users\Public\Downloads\$($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 within the current user's appdata.
    $ResolveWingetPath = Join-Path $env:USERPROFILE 'AppData\Local\Microsoft\WindowsApps\winget.exe'

    if ($ResolveWingetPath){ #Checks if ResolveWingetPath has data within it

        #Writes in the console what the value of WingetPath is
        Write-Host "winget.exe found at: $ResolveWingetPath"
    }

    else {

        #If unable to find Winget Path. It will present this in the console.
        Throw "winget.exe not found in the current user's profile."

    }

    #'&' invokes/executes Winget and allows us to use winget linked arguements such as 'install'
    #Attempts to install Winget application and presents current winget version
    & $ResolveWingetPath --version
    & $ResolveWingetPath 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 

Uninstall Applications Via USER behaviour:

Uninstall Arguments:

powershell.exe -ExecutionPolicy Bypass -File .\WingetUninstallAsUser.ps1 -WingetPackageID “ENTERWINGETID”

Param(
    [Parameter(Mandatory=$true)]
    [string]$WingetPackageID
)

Start-Transcript -Path "C:\Users\Public\Downloads\$($WingetPackageID)_Install.log" -Append
Write-Host "Starting Transcript for Winget Installation" -ForegroundColor Green

try {

    Write-Host "Attempting to uninstall $($WingetPackageID) via Winget"
    $ResolveWingetPath = Join-Path $env:USERPROFILE 'AppData\Local\Microsoft\WindowsApps\winget.exe'

    if ($ResolveWingetPath){


        Write-Host "winget.exe found at: $ResolveWingetPath"
    }

    else {

        Throw "winget.exe not found in the current user's profile."
    }

    & $ResolveWingetPath uninstall --exact $WingetPackageID --silent --accept-source-agreements --disable-interactivity       
}

Catch {

    Throw "Failed to install Package. [$($_.Exception.Message)] See: %LOCALAPPDATA%\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\DiagOutputDir"
}

Stop-Transcript

Loading