≡ Menu

PowerShell : Comparing two file versions

$file1version = [version](Get-Command .File1.dll).FileVersionInfo.FileVersion            

$file2version = [version](Get-Command .File2.dll).FileVersionInfo.FileVersion            

if($file1version -eq $file2version) {            

write-host "File version numbers are equal"            

} else {            

write-host "File version numbers are not equal"            

}

Above simple code compares file versions of two files. The comparison here is very straight forward. In the above code it first queries the file versions of two files, File1 and File2 and compares both of them to see if they are equal or not.

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)            
}

Above code is little bit enhanced which checks for file existence and returns the version numbers in object format which can be further used for other version control related operations.