views:

5230

answers:

7

How can you get the version information from a .dll or .exe file in PowerShell?

Specifically interested in File Version, though other version info (i.e. Company, Language, Product Name, etc) would be helpful as well.

+1  A: 
[System.Diagnostics.FileVersionInfo]::GetVersionInfo("Path\To\File.dll")
EBGreen
+6  A: 

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($_).LegalCopyright }

Or even nicer as a script: http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!125.entry

Lars Truijens
See @Jaykul for a solution that does not require a .NET object. IMHO Jaykul's response should have been selected as the answer :)
Thomas Bratt
+1  A: 

As EBGreen said, [System.Diagnostics.FileVersionInfo]::GetVersionInfo(path) will work, but remember that you can also get all the members of FileVersionInfo, for example:

[System.Diagnostics.FileVersionInfo]::GetVersionInfo(path).CompanyName

You should be able to use every member of FileVersionInfo documented here, which will get you basically anything you could ever want about the file.

Adam Haile
+4  A: 

I prefer to install the PowerShell Community Extensions and just use the Get-FileVersionInfo function that it provides.

Like so:

Get-FileVersionInfo MyAssembly.dll
with output like:
ProductVersion   FileVersion      FileName
--------------   -----------      --------
1.0.2907.18095   1.0.2907.18095   C:\Path\To\MyAssembly.dll

I've used it against an entire directory of assemblies with great success.

David Mohundro
I like that answer. :-)
Keith Hill
+11  A: 

Try using the built-in command instead:

(Get-Command C:\Path\YourFile.Dll).FileVersionInfo

or

dir *.dll,*.exe | %{gcm $_.FullName} | select -expand File*
Jaykul
A: 

These only get the first 3 parts of the file version... ex: 1.9.2 instead of 1.9.2.1234

David
In PowerShell, when I run `set-content -path 'test.cs' '[assembly: System.Reflection.AssemblyVersion("1.2.3.4")]'; C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /target:library test.cs; [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$pwd\test.dll")`, I see the full `1.2.3.4` version number.
Emperor XLII
+4  A: 

'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 | fl


OriginalFilename : write
FileDescription  : Windows Write
ProductName      : Microsoft® Windows® Operating System
Comments         :
CompanyName      : Microsoft Corporation
FileName         : C:\Windows\write.exe
FileVersion      : 6.1.7600.16385 (win7_rtm.090713-1255)
ProductVersion   : 6.1.7600.16385
IsDebug          : False
IsPatched        : False
IsPreRelease     : False
IsPrivateBuild   : False
IsSpecialBuild   : False
Language         : 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.exe
6.1.7600.16385   6.1.7600.1638... C:\Windows\explorer.exe
6.1.7600.16385   6.1.7600.1638... C:\Windows\fveupdate.exe
6.1.7600.16385   6.1.7600.1638... C:\Windows\HelpPane.exe
6.1.7600.16385   6.1.7600.1638... C:\Windows\hh.exe
6.1.7600.16385   6.1.7600.1638... C:\Windows\notepad.exe
6.1.7600.16385   6.1.7600.1638... C:\Windows\regedit.exe
6.1.7600.16385   6.1.7600.1638... C:\Windows\splwow64.exe
1,7,0,0          1,7,0,0          C:\Windows\twunk_16.exe
1,7,1,0          1,7,1,0          C:\Windows\twunk_32.exe
6.1.7600.16385   6.1.7600.1638... C:\Windows\winhlp32.exe
6.1.7600.16385   6.1.7600.1638... C:\Windows\write.exe
xcud