Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 在PowerShell中获取文件版本_File_Powershell_Versioninfo - Fatal编程技术网

File 在PowerShell中获取文件版本

File 在PowerShell中获取文件版本,file,powershell,versioninfo,File,Powershell,Versioninfo,如何从PowerShell中的.dll或.exe文件中获取版本信息 我特别感兴趣的是文件版本,尽管其他版本信息(即公司、语言、产品名称)也会有所帮助。由于PowerShell可以调用类,您可以执行以下操作: [System.Diagnostics.FileVersionInfo]::GetVersionInfo("Path\To\File.dll") [System.Diagnostics.FileVersionInfo]::GetVersionInfo("somefilepath").Fil

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

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

由于PowerShell可以调用类,您可以执行以下操作:

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("Path\To\File.dll")
[System.Diagnostics.FileVersionInfo]::GetVersionInfo("somefilepath").FileVersion
或在文件列表中:

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

或者更好的脚本:

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

[System.Diagnostics.FileVersionInfo]::GetVersionInfo(path).CompanyName
您应该能够使用这里介绍的FileVersionInfo的每个成员,这将基本上为您提供您想要的关于该文件的任何信息。

我更喜欢安装,并且只使用它提供的get FileVersionInfo函数

像这样:

Get-FileVersionInfo MyAssembly.dll 获取FileVersionInfo MyAssembly.dll 输出如下:

ProductVersion FileVersion FileName -------------- ----------- -------- 1.0.2907.18095 1.0.2907.18095 C:\Path\To\MyAssembly.dll ProductVersion文件版本文件名 -------------- ----------- -------- 1.0.2907.18095 1.0.2907.18095 C:\Path\To\MyAssembly.dll
我对整个程序集目录使用了它,并取得了巨大的成功。

现在,您可以从get Item或get ChildItem获取FileVersionInfo,但它将显示附带产品的原始文件版本,而不是更新版本。例如:

(Get-Item C:\Windows\System32\Lsasrv.dll).VersionInfo.FileVersion
有趣的是,您可以通过以下方式获得更新(修补)的ProductVersion:

(Get-Command C:\Windows\System32\Lsasrv.dll).Version
我在“原始”和“修补”之间所做的区别基本上是由于文件版本的计算方式()。基本上从Vista开始,Windows API GetFileVersionInfo就从语言无关的文件(exe/dll)查询部分版本信息,从特定语言的mui文件(不会在文件每次更改时更新)查询非固定部分

因此,对于像lsasrv这样的文件(由于2014年11月SSL/TLS/RDS中的安全问题而被替换),这两个命令报告的版本(至少在该日期之后的一段时间内)是不同的,第二个是更“正确”的版本

然而,尽管它在LSASrv中是正确的,但ProductVersion和FileVersion可能不同(事实上,这是常见的)。因此,直接从部件文件获取更新文件版本的唯一方法是从零件中自行构建,如下所示:

Get-Item C:\Windows\System32\Lsasrv.dll | ft FileName, File*Part
或者通过从以下位置提取数据:

[System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName)
通过更新PowerShell中的TypeData,您可以轻松地将其添加到所有FileInfo对象中:

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
属性,显示更新后的文件版本…

“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

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

(get-item .\filename.exe).VersionInfo | FL
您还可以从VersionInfo中获取任何特定属性,因此:

(get-item .\filename.exe).VersionInfo.FileVersion
这与dir技术非常接近。

我发现这很有用:

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-Command C:\Path\YourFile.Dll).FileVersionInfo.FileVersion

这里有另一种方法。它使用Get-WmiObject CIM_数据文件来选择版本

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

我意识到这已经得到了回答,但如果有人对键入更少的字符感兴趣,我相信这是在PS v3+中编写这篇文章的最短方法:

ls application.exe | % versioninfo
  • ls
    Get ChildItem
  • %
    ForEach对象的别名
  • versioninfo
    这里是一种书写
    {$\u.versioninfo}
以这种方式使用
ls
的好处是,您可以轻松地调整它,以便在子文件夹中查找给定的文件。例如,以下命令将返回子文件夹中名为
application.exe
的所有文件的版本信息:

ls application.exe -r | % versioninfo
  • -r
    -Recurse
您可以通过添加
-ea silentlycontinue
来进一步完善此功能,以忽略无法搜索的文件夹中的权限错误:

ls application.exe -r -ea silentlycontinue | % versioninfo
  • -ea
    -ErrorAction
最后,如果您的结果中有省略号(…),可以附加
|fl
以不同的格式返回信息。这将返回更多详细信息,尽管格式为列表,而不是每个结果一行:

ls application.exe -r -ea silentlycontinue | % versioninfo | fl
  • fl
    格式列表的别名
我意识到这与xcud的回答非常相似,因为
ls
dir
都是
Get ChildItem
的别名。但我希望我的“最短”方法能帮助别人

最后一个示例可以用以下方式长时间编写:

Get-ChildItem -Filter application.exe -Recurse -ErrorAction SilentlyContinue | ForEach-Object {$_.VersionInfo} | Format-List

。。。但我认为我的方式更酷,对一些人来说,更容易记住。(但大多更酷)。

有关不需要.NET对象的解决方案,请参阅@Jaykul。IMHO Jaykul的回答应该被选为答案:)虽然其他答案给出了更短的命令,但我尝试打印的所有答案都输出了过多的信息,并将文件路径截断为“…”。这个答案中的第二个命令给出了您所需要的内容,适用于文件目录,并且以一种很容易看到如何修改它以返回其他信息的方式进行格式化。只需将命令中的.LegalCopyright更改为.FileVersion。这是.NET EXE的正确版本。Jaykul的答案没有得到相同的版本。这实际上是不对的。查看
get item C:\Windows\System32\ubpm.dll |%VersionInfo | fl*-force
,并将FilePrivatePart与FileVersion的最后一部分进行比较。FileVersion显示的是最初发布的版本,而不是经过修补的版本。另一方面,此命令显示pa