.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 [WayBack] FileVersion or [WayBack] ProductVersion properties of a [WayBack] FileVersionInfo Class (System.Diagnostics) are both strings, so do not always contain a set of four dot-separated numbers
- The accurate numbers are in these properties in the right order that have the correct information in integer typed properties:
- Both
FileVersionandProductVersionare sometimes obtained from the MUI ([WayBack] Multilingual User Interface – Wikipedia) which isn’t always updated when the PE file is. However, the integer typed properties are.
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:
- [WayBack] winapi – GetFileVersionInfo() returns wrong file’s version information – Stack Overflow
- [WayBack] Getting Accurate File Versions | Rohn’s Blog
- [WayBack] Why doesn’t the File Version match up correctly when I use Get-ItemProperty?
- [WayBack] How to (correctly) check file versions with PowerShell | Ask Premier Field Engineering (PFE) Platforms
–jeroen






Leave a comment