1

I'm trying to use the following code to compare a file's version to a specified version, and tell me which one is higher.

function Get-FileVersionInfo            
{            
  param(            
    [Parameter(Mandatory=$true)]            
     [string]$FileName)            

  if(!(test-path $filename)) {            
  write-host "File not found"            
  return $null            
  }            

  return [System.Diagnostics.FileVersionInfo]::GetVersionInfo($FileName)            

}

$file = Get-FileVersionInfo("C:\program files\internet explorer\iexplore.exe")


if($file.ProductVersion -gt "11.00.9600.17840") {
    echo "file is higher version"
}
elseif($file.ProductVersion -eq "11.00.9600.17840") {
    echo "file is equal version"
}
else {
    echo "file is lower version"
}

echo "Product version is:" $file.ProductVersion

FYI using ProductVersion instead of FileVersion because FileVersion seems to contain extra data sometimes.

It returns "file is a lower version" even though that's the same version that is displayed in Properties.

Do I need to do something else to get it to compare the ProductVersion property to a string?

1
  • 3
    All you've to do is casting your version string as version if($file.ProductVersion -gt [version]"11.00.9600.17840") { Commented Sep 14, 2017 at 15:40

2 Answers 2

6

You don't compare that Property to a string. Create a [System.Version]-object from the string.

fixed code:

    $version = [System.Version]::Parse("11.00.9600.17840")
if($file.ProductVersion -gt $version) {
    echo "file is higher version"
}
elseif($file.ProductVersion -eq $version) {
    echo "file is equal version"
}
else {
    echo "file is lower version"
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can also use ProductVersionRaw which does this.
0

PowerShell functions don't work like typical other language functions. You don't "return" a value. Instead, you output it to the Pipeline for display or further processing using Write-Output. If you do it correctly, the output will be an actual System.Diagnostics.FileVersionInfo object from which you can compare versions. See revised code below.

function Get-FileVersionInfo            
{   
  [CmdletBinding()]         
  param(            
    [Parameter(Mandatory=$true)]            
    [string]$FileName)            

  if(!(test-path $filename)) {            
    Write-Error "File not found"
  }            

  Write-Output ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($FileName))

}

$version = Get-FileVersionInfo -FileName "C:\Program Files\Internet Explorer\iexplore.exe"
$version | Get-Member

$version.FileMajorPart
$version.FileMinorPart
$version.FileVersion

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.