Forum Discussion
CS_HelpDesk
2 years agoGoTo Contributor
Install application from the web by the application's ID and version.
Without specifying the version, winget installs the latest version of the application.
You can find the ID of the app via the 'winget search' command.
---
$appId = $null
$version = $null
$PlatformString = if([Environment]::Is64BitOperatingSystem){"x64"} else{"x86"}
function IsVistualMachine
{
try
{
if((Get-CimInstance win32_computersystem).model -like "*virtual*"){return $true}
}
catch{}
$false
}
function Get-RegUninstallKey
{
param (
[string]$DisplayName
)
$ErrorActionPreference = 'Continue'
$uninstallKeys = @("registry::HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall")
if($PlatformString -eq "x64"){$uninstallKeys += "registry::HKLM\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"}
$softwareTable = @()
foreach ($key in $uninstallKeys)
{
$softwareTable += Get-Childitem $key | Get-ItemProperty | where displayname | Sort-Object -Property displayname
}
if ($DisplayName)
{
$softwareTable | where displayname -Like "*$DisplayName*"
}
else
{
$softwareTable | Sort-Object -Property displayname -Unique
}
}
function IsVisualCppRedistrubutable2019OrLaterInstalled
{
$keys = Get-RegUninstallKey -DisplayName "Microsoft Visual C++ * Redistributable ($PlatformString)"
foreach ($key in $keys){
if([System.Version]$key.BundleVersion -ge [System.Version]"14.29.0.0"){return $true} # version 2015-2019
}
$false
}
function Install-VisualC {
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($url, "$env:Temp\vc_redist.exe")
$WebClient.Dispose()
start-process "$env:temp\vc_redist.exe" -argumentlist "/q /norestart" -Wait
}
# If Visual C++ Redist. not installed, install it
if ((IsVistualMachine) -and -not (IsVisualCppRedistrubutable2019OrLaterInstalled))
{
Write-Host -message "Suitable version of Visual C++ not found. Attempting to install"
try {
Install-VisualC
}
Catch [System.InvalidOperationException]{
Write-Host -message "Error installing Visual C++ redistributable. Attempting install once more"
Start-Sleep -Seconds 5
Install-VisualC
}
Catch {
Write-Host -message "Failed to install Visual C++ redistributable!"
Write-Host -message $_
exit 1
}
if (-not (IsVisualCppRedistrubutable2019OrLaterInstalled)){Write-Host -message "Suitable version of Visual C++ Redistributable not found!" ; exit 1}
else {Write-Host -message "Successfully installed Microsoft Visual C++ Redistributable ($PlatformString)"}
}
function GetWingetPath
{
$WingetPath = (Get-ChildItem "$env:programfiles\WindowsApps" -Recurse -File
| where { $_.name -like "AppInstallerCLI.exe" -or $_.name -like "Winget.exe" }
| select -ExpandProperty fullname
| Sort-Object -Property @{Expression={[Version][RegEx]::Match($_, "_(\d+.\d+.\d+.\d+)_").Groups[1].Value}; Descending=$true})
if ($WingetPath.count -gt 1) { $WingetPath = $WingetPath[0] }
$WingetPath
}
$winget = GetWingetPath
if($null -eq $winget){
Write-Host "Winget not found!"
exit 1
}
if($version)
{
&$winget install --id $appId -v $version --accept-package-agreements --accept-source-agreements --silent
}
else
{
&$winget install --id $appId --accept-package-agreements --accept-source-agreements --silent
}