Get file version in PowerShell Get file version in PowerShell powershell powershell

Get file version in PowerShell


Nowadays you can get the FileVersionInfo from Get-Item or Get-ChildItem, but it will show the original FileVersion from the shipped product, and not the updated version. For instance:

(Get-Item C:\Windows\System32\Lsasrv.dll).VersionInfo.FileVersion

Interestingly, you can get the updated (patched) ProductVersion by using this:

(Get-Command C:\Windows\System32\Lsasrv.dll).Version

The distinction I'm making between "original" and "patched" is basically due to the way the FileVersion is calculated (see the docs here). Basically ever since Vista, the Windows API GetFileVersionInfo is querying part of the version information from the language neutral file (exe/dll) and the non-fixed part from a language-specific mui file (which isn't updated every time the files change).

So with a file like lsasrv (which got replaced due to security problems in SSL/TLS/RDS in November 2014) the versions reported by these two commands (at least for a while after that date) were different, and the second one is the more "correct" version.

However, although it's correct in LSASrv, it's possible for the ProductVersion and FileVersion to be different (it's common, in fact). So the only way to get the updated Fileversion straight from the assembly file is to build it up yourself from the parts, something like this:

Get-Item C:\Windows\System32\Lsasrv.dll | ft FileName, File*Part

Or by pulling the data from this:

[System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName)

You can easily add this to all FileInfo objects by updating the TypeData in PowerShell:

Update-TypeData -TypeName System.IO.FileInfo -MemberName FileVersion -MemberType ScriptProperty -Value {   [System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName) | % {      [Version](($_.FileMajorPart, $_.FileMinorPart, $_.FileBuildPart, $_.FilePrivatePart)-join".")    }}

Now every time you do Get-ChildItem or Get-Item you'll have a FileVersion property that shows the updated FileVersion ...


Since PowerShell can call .NET classes, you could do the following:

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("somefilepath").FileVersion

Or as noted here on a list of files:

get-childitem * -include *.dll,*.exe | foreach-object { "{0}`t{1}" -f $_.Name, [System.Diagnostics.FileVersionInfo]::GetVersionInfo($_).FileVersion }

Or even nicer as a script: https://jtruher3.wordpress.com/2006/05/14/powershell-and-file-version-information/


'dir' is an alias for Get-ChildItem which will return back a System.IO.FileInfo class when you're calling it from the filesystem which has VersionInfo as a property. So ...

To get the version info of a single file do this:

PS C:\Windows> (dir .\write.exe).VersionInfo | flOriginalFilename : writeFileDescription  : Windows WriteProductName      : Microsoft® Windows® Operating SystemComments         :CompanyName      : Microsoft CorporationFileName         : C:\Windows\write.exeFileVersion      : 6.1.7600.16385 (win7_rtm.090713-1255)ProductVersion   : 6.1.7600.16385IsDebug          : FalseIsPatched        : FalseIsPreRelease     : FalseIsPrivateBuild   : FalseIsSpecialBuild   : FalseLanguage         : English (United States)LegalCopyright   : © Microsoft Corporation. All rights reserved.LegalTrademarks  :PrivateBuild     :SpecialBuild     :

For multiple files this:

PS C:\Windows> dir *.exe | %{ $_.VersionInfo }ProductVersion   FileVersion      FileName--------------   -----------      --------6.1.7600.16385   6.1.7600.1638... C:\Windows\bfsvc.exe6.1.7600.16385   6.1.7600.1638... C:\Windows\explorer.exe6.1.7600.16385   6.1.7600.1638... C:\Windows\fveupdate.exe6.1.7600.16385   6.1.7600.1638... C:\Windows\HelpPane.exe6.1.7600.16385   6.1.7600.1638... C:\Windows\hh.exe6.1.7600.16385   6.1.7600.1638... C:\Windows\notepad.exe6.1.7600.16385   6.1.7600.1638... C:\Windows\regedit.exe6.1.7600.16385   6.1.7600.1638... C:\Windows\splwow64.exe1,7,0,0          1,7,0,0          C:\Windows\twunk_16.exe1,7,1,0          1,7,1,0          C:\Windows\twunk_32.exe6.1.7600.16385   6.1.7600.1638... C:\Windows\winhlp32.exe6.1.7600.16385   6.1.7600.1638... C:\Windows\write.exe