在PowerShell中获取文件版本

如何从PowerShell中的.dll.exe文件获取版本信息?

我特别感兴趣的File Version ,但其他版本的信息(即CompanyLanguageProduct Name等)也将有所帮助。

由于PowerShell可以调用.NET类,因此可以执行以下操作:

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

或者如文件列表中所述:

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

甚至更好的脚本: http : //jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!125.entry

现在,您可以从Get-Item或Get-ChildItem中获取FileVersionInfo,但它将显示已发货产品中的原始FileVersion,而不是已更新的版本。 例如:

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

有趣的是,你可以通过使用这个获得更新的(补丁) ProductVersion

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

请注意,对于像lsasrv(由于2014年11月的SSL / TLS / RDS中的安全问题而被replace)的文件,这两个命令报告的版本是不同的, 第二个是正确的版本。

但是,虽然在LSASrv中是正确的,但是ProductVersion和FileVersion可能是不同的(事实上这很常见)。 所以获得更新的 Fileversion的唯一方法就是自己构build它,如下所示:

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

或者说,是这样的:

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

现在,每当您执行Get-ChildItemGet-Item您将拥有一个显示更新的FileVersion的FileVersion属性…

'dir'是Get-ChildItem的别名,当你从具有VersionInfo属性的文件系统调用它时,它将返回一个System.IO.FileInfo类。 所以…

要获取单个文件的版本信息,请执行以下操作:

 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 : 

对于多个文件:

 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 

我更喜欢安装PowerShell社区扩展 ,只使用它提供的Get-FileVersionInfo函数。

像这样:

  Get-FileVersionInfo MyAssembly.dll 

输出如下:

 ProductVersion FileVersion FileName
 -------------- ----------- --------
 1.0.2907.18095 1.0.2907.18095 C:\ Path \ To \ MyAssembly.dll

我已经用它对整个目录的组件很成功。

另一种方法是使用内置的文件访问技术:

 (get-item .\filename.exe).VersionInfo | FL 

您也可以从VersionInfo获得任何特定的属性,因此:

 (get-item .\filename.exe).VersionInfo.FileVersion 

这与dir技术非常接近。

这是基于其他的答案,但正是我所追求的:

 (Get-Command C:\Path\YourFile.Dll).FileVersionInfo.FileVersion 
 [System.Diagnostics.FileVersionInfo]::GetVersionInfo("Path\To\File.dll") 

正如EBGreen所说的,[System.Diagnostics.FileVersionInfo] :: GetVersionInfo(path)将起作用,但请记住,您也可以获取FileVersionInfo的所有成员,例如:

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

您应该能够使用这里logging的FileVersionInfo的每个成员,这将基本上得到你想要关于该文件的任何东西。

我觉得这很有用:

 function Get-Version($filePath) { $name = @{Name="Name";Expression= {split-path -leaf $_.FileName}} $path = @{Name="Path";Expression= {split-path $_.FileName}} dir -recurse -path $filePath | % { if ($_.Name -match "(.*dll|.*exe)$") {$_.VersionInfo}} | select FileVersion, $name, $path } 

这里是另一种方法。 它使用Get-WmiObject CIM_DATAFILE来select版本。

 (Get-WmiObject -Class CIM_DataFile -Filter "Name='C:\\Windows\\explorer.exe'" | Select-Object Version).Version