在 PowerShell 中获取文件版本
如何从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这里有一个替代方法。 它使用 Get-WmiObject CIM_DATAFILE 来选择版本。
Here an alternative method. It uses Get-WmiObject CIM_DATAFILE to select the version.
我发现这很有用:
I find this useful:
正如 EBGreen 所说, [System.Diagnostics.FileVersionInfo]::GetVersionInfo(path) 可以工作,但请记住,您还可以获取 FileVersionInfo 的所有成员,例如:
您应该能够使用此处记录的 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:
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.
我更喜欢安装 PowerShell 社区扩展 并仅使用它提供的 Get-FileVersionInfo 函数。
像这样:
输出如下:
我已经将它用于整个程序集目录,并取得了巨大成功。
I prefer to install the PowerShell Community Extensions and just use the Get-FileVersionInfo function that it provides.
Like so:
with output like:
I've used it against an entire directory of assemblies with great success.
自 Windows 10 中的 PowerShell 5 起,您可以在
Get-Item
或Get-ChildItem
的输出上查看FileVersionRaw
(或 ProductVersionRaw),例如这:它实际上与下面原始答案中我的
Update-TypeData
相同的ScriptProperty
,但现在是内置的。在 PowerShell 4 中,您可以从 Get-Item 或 Get-ChildItem 获取 FileVersionInfo,但它会显示发货产品中的原始 FileVersion,而不是更新的版本。 例如:
有趣的是,您可以使用以下方法获取更新(修补的)ProductVersion:
我在“原始”和“修补”之间所做的区别基本上是由于 FileVersion 的计算方式造成的( 请参阅此处的文档) 。 基本上从 Vista 开始,Windows API GetFileVersionInfo 就从语言中性文件 (exe/dll) 中查询部分版本信息,并从特定于语言的 mui 文件中查询非固定部分(该文件不会在每次文件更改时更新) )。
因此,对于像 lsasrv 这样的文件(由于 2014 年 11 月 SSL/TLS/RDS 中的安全问题而被替换),这两个命令报告的版本(至少在该日期之后的一段时间内)是不同的,并且第二个命令报告的版本是不同的。一个是更“正确”的版本。
然而,尽管在 LSASrv 中它是正确的,但 ProductVersion 和 FileVersion 可能不同(事实上,这很常见)。 因此,直接从程序集文件获取更新文件版本的唯一方法是从零件中自行构建它,如下所示:
或者通过从中提取数据:
您可以轻松地将其添加到所有通过更新 PowerShell 中的 TypeData 来获取 FileInfo 对象:
现在,每次执行
Get-ChildItem
或Get-Item
操作时,您都会有一个FileVersionRaw
属性,该属性显示更新的文件版本...Since PowerShell 5 in Windows 10, you can look at
FileVersionRaw
(or ProductVersionRaw) on the output ofGet-Item
orGet-ChildItem
, like this:It's actually the same
ScriptProperty
from myUpdate-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:
Interestingly, you could get the updated (patched) ProductVersion by using this:
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:
Or by pulling the data from this:
You can easily add this to all FileInfo objects by updating the TypeData in PowerShell:
Now every time you do
Get-ChildItem
orGet-Item
you'll have aFileVersionRaw
property that shows the updated File Version ...我意识到这个问题已经得到解答,但如果有人有兴趣输入更少的字符,我相信这是在 PS v3+ 中编写此内容的最短方法:
ls
是Get-ChildItem< 的别名/code>
%
是ForEach-Object
versioninfo
的别名,{$_.VersionInfo}< 的简写方式/code>
以这种方式使用 ls 的好处是您可以轻松地调整它以在子文件夹中查找给定文件。 例如,以下命令将返回子文件夹中名为
application.exe
的所有文件的版本信息:-r
是-Recurse
可以通过添加-easilentlycontinue
来进一步完善此功能,以忽略无法搜索的文件夹中的权限错误等内容:-ea
是-ErrorAction
的别名最后,如果结果中出现省略号 (...),您可以附加
| fl
以不同的格式返回信息。 这会返回更多详细信息,尽管格式化为列表,而不是每个结果一行:fl
是Format-List
的别名我意识到这与 xcud 非常相似回复
ls
和dir
都是Get-ChildItem
的别名。 但我希望我的“最短”方法能帮助别人。最后的例子可以用以下方式手写:
...但我认为我的方式更酷,而且对于某些人来说更容易记住。 (但大多更酷)。
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
is an alias forGet-ChildItem
%
is an alias forForEach-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 calledapplication.exe
within subfolders:-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:-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:fl
is an alias forFormat-List
I realise this is very similar to xcud's reply in that
ls
anddir
are both aliases forGet-ChildItem
. But I'm hoping my "shortest" method will help someone.The final example could be written in long-hand in the following way:
... but I think my way is cooler and, for some, easier to remember. (But mostly cooler).
'dir' 是 Get-ChildItem 的别名,当您从具有 VersionInfo 作为属性的文件系统调用它时,它将返回 System.IO.FileInfo 类。 所以...
要获取单个文件的版本信息,请执行以下操作:
对于多个文件,请执行以下操作:
'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:
For multiple files this:
由于 PowerShell 可以调用 .NET 类,因此您可以执行以下操作:
或者作为 此处注明 在文件列表上:
或者作为脚本更好: https://jtruher3.wordpress.com/2006/05/14/powershell-and-file-version-information/
Since PowerShell can call .NET classes, you could do the following:
Or as noted here on a list of files:
Or even nicer as a script: https://jtruher3.wordpress.com/2006/05/14/powershell-and-file-version-information/
这是基于其他答案,但正是我所追求的:
This is based on the other answers, but is exactly what I was after:
另一种方法是使用内置文件访问技术:
您还可以从 VersionInfo 获取任何特定属性,因此:
这与 dir 技术非常接近。
Just another way to do it is to use the built-in file access technique:
You can also get any particular property off the VersionInfo, thus:
This is quite close to the dir technique.