NickLab
NickLab

Reputation: 31

Powershell - Condition to verify correct version of an AppX is installed

I'm trying to create a Powershell script in which it will automate the installation of applications and printers needed for work. Most of the script works great, the only thing that doesn't is a Condition that I've placed to skip a step.

The script uses WinGet to install most of the applications. WinGet requires the Microsoft Desktop App Installer version 1.12.11692.0 or higher for Winget to work. If that version is not installed, then install it and then continue with the script. If it is installed, then continue with the script without prompting the user to install Microsoft Desktop App Installer.

$AppInstallerVersion = Get-AppxPackage Microsoft.DesktopAppInstaller | Select Version

Write-Host "Microsoft Desktop App Installer Version:" $AppInstallerVersion


if ($AppInstallerVersion -eq "1.12.11692.0")
{
 Write-Host "Microsoft Desktop App Installer is equal or higher to the version needed for this script to work, continuing the script."

 .\(pathtoInstallationscript).ps1
}
else
{
Write-Host "Microsoft Desktop App Installer does not meet the minimum version to run this script, a window will now appear to install the version required for this script the run. Click on Update to install it.
Once installed, press the Enter key in the script to continue."

.\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle
pause
.\(PathtoInstallationscript).ps1
}

So far I've tried the following after the -eq part of the condition:

The odd thing is that it would oddly sometimes work, but when testing out the script a 2nd time, it would just break.

Upvotes: 1

Views: 3024

Answers (2)

NickLab
NickLab

Reputation: 31

I've solved it, by changing the variable $AppInstallerVersion and the Condition itself. It took a lot of trial and error but it's now working! The hint was that because [Version] couldn't be converted. It wasn't an actually string or System.Version. Rather it was a PSObject so it had to be changed to .Version which only prints out the version number. Which can then be compared to a string.

$AppInstallerVersion = Get-AppxPackage Microsoft.DesktopAppInstaller

if ($AppInstallerVersion.Version -ge "1.12.11692.0")

Upvotes: 2

Justin
Justin

Reputation: 1503

You are using an equality but you seem to assume it means equal or greater, and to @Olaf's point you may want to cast the version strings to the [Version] type.

So I would suggest trying something like this:

if ([Version]$AppInstallerVersion -ge [Version]"1.12.11692.0") {
  Write-Host "Microsoft Desktop App Installer is equal or higher to the version needed for this script to work, continuing the script."

 .\(pathtoInstallationscript).ps1
}

Upvotes: 1

Related Questions