在 PowerShell 中获取文件版本

发布于 2024-07-04 14:56:14 字数 175 浏览 14 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

这里有一个替代方法。 它使用 Get-WmiObject CIM_DATAFILE 来选择版本。

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

Here an alternative method. It uses Get-WmiObject CIM_DATAFILE to select the version.

(Get-WmiObject -Class CIM_DataFile -Filter "Name='C:\\Windows\\explorer.exe'" | Select-Object Version).Version
囍笑 2024-07-11 14:56:14

我发现这很有用:

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
}

I find this useful:

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
}
锦欢 2024-07-11 14:56:14

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

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

您应该能够使用此处记录的 FileVersionInfo 的每个成员,其中基本上将为您提供有关该文件的所有信息。

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.

辞旧 2024-07-11 14:56:14

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

像这样:

Get-FileVersionInfo MyAssembly.dll

输出如下:

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

我已经将它用于整个程序集目录,并取得了巨大成功。

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.

深爱成瘾 2024-07-11 14:56:14

自 Windows 10 中的 PowerShell 5 起,您可以在 Get-ItemGet-ChildItem 的输出上查看 FileVersionRaw(或 ProductVersionRaw),例如这:

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

它实际上与下面原始答案中我的 Update-TypeData 相同的 ScriptProperty ,但现在是内置的。

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

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

有趣的是,您可以使用以下方法获取更新(修补的)ProductVersion

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

我在“原始”和“修补”之间所做的区别基本上是由于 FileVersion 的计算方式造成的( 请参阅此处的文档) 。 基本上从 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 FileVersionRaw -MemberType ScriptProperty -Value {
   [System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName) | % {
      [Version](($_.FileMajorPart, $_.FileMinorPart, $_.FileBuildPart, $_.FilePrivatePart)-join".") 
   }
}

现在,每次执行 Get-ChildItemGet-Item 操作时,您都会有一个 FileVersionRaw 属性,该属性显示更新的文件版本...

Since PowerShell 5 in Windows 10, you can look at FileVersionRaw (or ProductVersionRaw) on the output of Get-Item or Get-ChildItem, like this:

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

It's actually the same ScriptProperty from my Update-TypeData in the original answer below, but built-in now.

In PowerShell 4, you could get the FileVersionInfo from Get-Item or Get-ChildItem, but it would show the original FileVersion from the shipped product, and not the updated version. For instance:

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

Interestingly, you could get the updated (patched) ProductVersion by using this:

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

The distinction I'm making between "original" and "patched" is basically due to the way the FileVersion is calculated (see the docs here). Basically ever since Vista, the Windows API GetFileVersionInfo is querying part of the version information from the language neutral file (exe/dll) and the non-fixed part from a language-specific mui file (which isn't updated every time the files change).

So 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 (at least for a while after that date) were different, and the second one is the more "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 straight from the assembly file is to build it up yourself from the parts, something like this:

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

Or by pulling the data from this:

[System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName)

You can easily add this to all FileInfo objects by updating the TypeData in PowerShell:

Update-TypeData -TypeName System.IO.FileInfo -MemberName FileVersionRaw -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 FileVersionRaw property that shows the updated File Version ...

心碎无痕… 2024-07-11 14:56:14

我意识到这个问题已经得到解答,但如果有人有兴趣输入更少的字符,我相信这是在 PS v3+ 中编写此内容的最短方法:

ls application.exe | % versioninfo
  • lsGet-ChildItem< 的别名/code>
  • %ForEach-Object versioninfo 的别名,
  • 这里是 {$_.VersionInfo}< 的简写方式/code>

以这种方式使用 ls 的好处是您可以轻松地调整它以在子文件夹中查找给定文件。 例如,以下命令将返回子文件夹中名为 application.exe 的所有文件的版本信息:

ls application.exe -r | % versioninfo
  • 的别名

-r-Recurse 可以通过添加 -easilentlycontinue 来进一步完善此功能,以忽略无法搜索的文件夹中的权限错误等内容:

ls application.exe -r -ea silentlycontinue | % versioninfo
  • -ea-ErrorAction 的别名最后

,如果结果中出现省略号 (...),您可以附加 | fl 以不同的格式返回信息。 这会返回更多详细信息,尽管格式化为列表,而不是每个结果一行:

ls application.exe -r -ea silentlycontinue | % versioninfo | fl
  • flFormat-List 的别名

我意识到这与 xcud 非常相似回复 lsdir 都是 Get-ChildItem 的别名。 但我希望我的“最短”方法能帮助别人。

最后的例子可以用以下方式手写:

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

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

I realise this has already been answered, but if anyone's interested in typing fewer characters, I believe this is the shortest way of writing this in PS v3+:

ls application.exe | % versioninfo
  • ls is an alias for Get-ChildItem
  • % is an alias for ForEach-Object
  • versioninfo here is a shorthand way of writing {$_.VersionInfo}

The benefit of using ls in this way is that you can easily adapt it to look for a given file within subfolders. For example, the following command will return version info for all files called application.exe within subfolders:

ls application.exe -r | % versioninfo
  • -r is an alias for -Recurse

You can further refine this by adding -ea silentlycontinue to ignore things like permission errors in folders you can't search:

ls application.exe -r -ea silentlycontinue | % versioninfo
  • -ea is an alias for -ErrorAction

Finally, if you are getting ellipses (...) in your results, you can append | fl to return the information in a different format. This returns much more detail, although formatted in a list, rather that on one line per result:

ls application.exe -r -ea silentlycontinue | % versioninfo | fl
  • fl is an alias for Format-List

I realise this is very similar to xcud's reply in that ls and dir are both aliases for Get-ChildItem. But I'm hoping my "shortest" method will help someone.

The final example could be written in long-hand in the following way:

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

... but I think my way is cooler and, for some, easier to remember. (But mostly cooler).

空‖城人不在 2024-07-11 14:56:14

'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

'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
Smile简单爱 2024-07-11 14:56:14

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

或者作为脚本更好: https://jtruher3.wordpress.com/2006/05/14/powershell-and-file-version-information/

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: https://jtruher3.wordpress.com/2006/05/14/powershell-and-file-version-information/

女皇必胜 2024-07-11 14:56:14
[System.Diagnostics.FileVersionInfo]::GetVersionInfo("Path\To\File.dll")
[System.Diagnostics.FileVersionInfo]::GetVersionInfo("Path\To\File.dll")
花开雨落又逢春i 2024-07-11 14:56:14

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

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

This is based on the other answers, but is exactly what I was after:

(Get-Command C:\Path\YourFile.Dll).FileVersionInfo.FileVersion
上课铃就是安魂曲 2024-07-11 14:56:14

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

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

您还可以从 VersionInfo 获取任何特定属性,因此:

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

这与 dir 技术非常接近。

Just another way to do it is to use the built-in file access technique:

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

You can also get any particular property off the VersionInfo, thus:

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

This is quite close to the dir technique.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文