Gordon
Gordon

Reputation: 6863

Get-Command & version number

I have a need to check to see if a program is installed by looking at an EXE file. Some goofy programs don't behave nicely and put rational info in the registry. I have Get-Command $path working, and it allows me to get version info as a property which is also nice. However, I want to do some validation, making sure the file is a valid file. So I tried taking a TXT file and just changing the extension to EXE, and kind of expected Get-Command to throw an exception. But no, it works fine and just reports a version number of 0.0.0.0. When I manually look at the properties of a real EXE and my fake EXE I see that File version and Product version both exist as properties, but are empty. I'm not casting the version value to [version] or anything, so it seems like Windows itself inserts a value of 0.0.0.0 when it's null, which... sucks. Is this something I can actually depend on; that no REAL executable will ever have a value of 0.0.0.0 for Version, and report that as a bad file accordingly? Or is 0.0.0.0 a valid version number that someone might actually use?

Upvotes: 2

Views: 1140

Answers (1)

mklement0
mklement0

Reputation: 437638

0.0.0.0 is technically a valid version number, but I'd say it's quite unlikely that an executable uses it intentionally.

Pragmatically speaking, you can therefore infer from the presence of this version number that a given executable contains no embedded version resource.[1]

$hasVersionResource = (Get-Command some.exe).Version -ne '0.0.0.0'

However, this aspect is separate from whether a given file is a valid (binary) executable, i.e. whether it can actually run: binaries do not require this resource in order to execute.


[1] Technically, the .Version property returns a 0.0.0.0 [version] instance in both the following cases: (a) a version-info resource is present with this (useless) version number or (b) a version-info resource is absent. If you need to distinguish between these two cases, examine the properties of .FileVersionInfo instead.

Upvotes: 3

Related Questions