Powershell - Comare the major versions of two files

alhowarthWF 301 Reputation points
2022-08-04T21:23:57.217+00:00

I'm trying to compare two versions of Edge. One is updated by others, and one I must manually update. I currently have a PowerShell script which grabs both versions and emails them to me:

    $Servername = $env:computername  
    $To2 = '******@work.com'  
    $InstVer=(Get-Command "C:\location 1\msedge.exe").Version  
    $PortVer=(Get-Command "C:\location 2\msedgedriver.exe").Version  
    {$smtps = 'a_mailserver.com'}  
    Send-MailMessage -SmtpServer $smtps -From '******@work.com' -To "$To2" -Subject "Edge browswer report from $Servername" -Body "On server $Servername, Edge version is $InstVer and Edge Driver version is $PortVer."  

This works very well, except I receive too many emails. I would prefer the script to compare the two versions and tell me if there is a difference in the Major version.

If I use a line like this to compare it states they are different:

$InstVer -gt $PortVer  

103.0.1264.49
103.0.1264.37
True

I found and tried the following code, which just uses hard-coded versions for comparisons:

[version]$SomeVersion='1.1.2'  
[version]$OtherVersion='1.1.1.0'  
  
$NormalizedScriptVersion = New-Object -TypeName System.Version -ArgumentList $SomeVersion.Major,$SomeVersion.Minor,$SomeVersion.Build  
$NormalizedCurrentVersion = New-Object -TypeName System.Version -ArgumentList $OtherVersion.Major,$OtherVersion.Minor,$OtherVersion.Build  
  
$NormalizedScriptVersion -gt $NormalizedCurrentVersion   

Same issue though, it compares all parts, Major, Minor and Build.
I even tried removing the .Minor and .Build arguments, but PowerShell complained.

Any ideas?

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rafael da Rocha 5,251 Reputation points
    2022-08-04T21:32:02.3+00:00
    $NormalizedScriptVersion.Major -gt $NormalizedCurrentVersion.Major   
    

    Would evaluate to False

    In your script you could use

    $InstVer=(Get-Command "C:\location 1\msedge.exe").Version.Major  
    $PortVer=(Get-Command "C:\location 2\msedgedriver.exe").Version.Major  
    

    or keep ".Version" and compare

    $InstVer.Major -gt $PortVer.Major  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-08-05T02:46:26.803+00:00

    In your test code, you have to cast the strings as 'version'.

    $SomeVersion=[version]'1.1.2'  
    $OtherVersion=[version]'1.1.1.0'  
      
    $SomeVersion.Major -gt $OtherVersion.Major  
      
    $SomeVersion=[version]'2.1.2'  
    $OtherVersion=[version]'1.1.1.0'  
      
    $SomeVersion.Major -gt $OtherVersion.Major  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.