在PowerShell中获取文件版本

时间:2020-03-05 18:43:58  来源:igfitidea点击:

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

我对"文件版本"特别感兴趣,尽管其他版本信息(即"公司","语言","产品名称"等)也会有所帮助。

解决方案

回答

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("Path\To\File.dll")

回答

由于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

回答

正如EBGreen所说,[System.Diagnostics.FileVersionInfo] :: GetVersionInfo(path)可以使用,但是请记住,我们还可以获取FileVersionInfo的所有成员,例如:

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

我们应该能够使用此处记录的FileVersionInfo的每个成员,这基本上可以为我们提供有关该文件的任何所需信息。

回答

我更喜欢安装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或者Get-ChildItem获取FileVersionInfo,但是它将显示出厂产品的原始FileVersion,而不是更新的版本。例如:

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

有趣的是,我们可以使用以下命令获取更新的(修补的)ProductVersion:

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

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

但是,尽管在LSASrv中是正确的,但ProductVersion和FileVersion可能会有所不同(实际上很常见)。因此,获取更新的Fileversion的唯一方法是自己构建它,如下所示:

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-ChildItem或者Get-Item时,我们都将拥有一个FileVersion属性,该属性显示更新的FileVersion ...

回答

" dir"是Get-ChildItem的别名,当我们从具有VersionInfo作为属性的文件系统中调用System.IO.FileInfo类时,它将返回该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