Get file version in PowerShell
How can you get the version information from a .dll
or .exe
file in PowerShell?
I am specifically interested in File Version
, though other version information (that is, Company
, Language
, Product Name
, etc.) would be helpful as well.
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: http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!125.entry
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:WindowsSystem32Lsasrv.dll).VersionInfo.FileVersion
Interestingly, you can get the updated (patched) ProductVersion by using this:
(Get-Command C:WindowsSystem32Lsasrv.dll).Version
Note that 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 are different, and the second one is the 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 is to build it up yourself, something like this:
Get-Item C:WindowsSystem32Lsasrv.dll | ft FileName, File*Part
Or rather, like this:
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 ...
'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:Windowswrite.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:Windowsbfsvc.exe
6.1.7600.16385 6.1.7600.1638... C:Windowsexplorer.exe
6.1.7600.16385 6.1.7600.1638... C:Windowsfveupdate.exe
6.1.7600.16385 6.1.7600.1638... C:WindowsHelpPane.exe
6.1.7600.16385 6.1.7600.1638... C:Windowshh.exe
6.1.7600.16385 6.1.7600.1638... C:Windowsnotepad.exe
6.1.7600.16385 6.1.7600.1638... C:Windowsregedit.exe
6.1.7600.16385 6.1.7600.1638... C:Windowssplwow64.exe
1,7,0,0 1,7,0,0 C:Windowstwunk_16.exe
1,7,1,0 1,7,1,0 C:Windowstwunk_32.exe
6.1.7600.16385 6.1.7600.1638... C:Windowswinhlp32.exe
6.1.7600.16385 6.1.7600.1638... C:Windowswrite.exe
链接地址: http://www.djcxy.com/p/4938.html
上一篇: 如何运行PowerShell脚本?
下一篇: 在PowerShell中获取文件版本