Get file/registry version number as a version object

Sometimes, version numbers of applications can’t be compared directly because they are interpreted as strings (where 10 comes after 1 but before 2).
A really simple way to work around this is to typecast the version number with [version].
$installVersion = [version]"58.0.3029.81";
$fileVersion = [version](Get-Item -Path $chromeExe).VersionInfo.ProductVersion;

if ($fileVersion -lt $installVersion) {
    #Upgrade
}
$registryVersion = [version]"0.0.0.0";

if (Test-Path -Path "HKLM:\$regUninstallPath\$ProductCode") {
    $registryVersion = [version](Get-ItemProperty -Path "HKLM:\$regUninstallPath\$roductCode").DisplayVersion;
}
#Note to reader: You would want to make sure to define all your variables; this code was pulled from a much longer script to give examples
TL;DR
Add “[version]” before your version numbers that are stored as a string to typecast them to a proper Windows version number.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s