The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

.NET and PowerShell: Getting proper version info from a PE file like EXE, DLL, assembly

Posted by jpluimers on 2019/08/01

I’ve learned the hard way that both .NET and PowerShell version information isn’t always accurate or usable for two reasons which I later found in various other blog and forum posts:

The easiest is to use these numbers to create a [WayBack] Version Class (System) instance using the [WayBack] Version Constructor (Int32, Int32, Int32, Int32) constructor. This has the added benefit that you directly compare versions with each other.

Sometimes it makes even sense to take the highest version from Product and File.

In PowerShell, this is the way to do that, assuming $imagePath points to a [WayBack] Portable Executable:

try {
  $VersionInfo = (Get-Item $imagePath).VersionInfo
  $FileVersion = [version]("{0}.{1}.{2}.{3}" -f $VersionInfo.FileMajorPart, $VersionInfo.FileMinorPart, $VersionInfo.FileBuildPart, $VersionInfo.FilePrivatePart)
  $ProductVersion = [version]("{0}.{1}.{2}.{3}" -f $VersionInfo.ProductMajorPart, $VersionInfo.ProductMinorPart, $VersionInfo.ProductBuildPart, $VersionInfo.ProductPrivatePart)
  $ActualVersion = $(if ($ProductVersion -gt $FileVersion) { $ProductVersion } else { $FileVersion })
}
catch {
  $ActualVersion = [version]("0.0.0.0")
}

Background information:

–jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.