Powershell属性值数据解析

时间:2014-03-11 14:41:18

标签: parsing powershell dir

我想创建一个powershell脚本,以从特定文件中提取特定属性的特定数据。所以简而言之。我想从.exe。

中获取Fileinfo属性中的FileVersion数据

我使用以下命令获取文件并从上面查看属性值。

dir c:\windows\system32\dfc.exe | fl versioninfo

输出结果如下

VersionInfo : File:             C:\windows\system32\dfc.exe
          InternalName:     dfcmnd.exe
          OriginalFilename: DFC.exe
          FileVersion:      7,30,220,3852
          FileDescription:  Command line utility for Deep Freeze 7.00
          Product:          Deep Freeze 7.00
          ProductVersion:   7.30.220.3852
          Debug:            False
          Patched:          False
          PreRelease:       False
          PrivateBuild:     False
          SpecialBuild:     False
          Language:         English (United States)

但我想要的只是

Fileversion: 7.30.220.3852

我无法想出一种方法来获取数据并丢弃其余数据。

感谢。

2 个答案:

答案 0 :(得分:2)

如果您只想要该值,则只需访问属性:

PS D:\> (get-item c:\windows\system32\write.exe).VersionInfo.FileVersion
6.1.7600.16385 (win7_rtm.090713-1255)

N.B。如果你使用通配符来匹配多个文件,那么它不会在Powershell v2上运行,但它可以在更新的Powershell版本上运行。

如果您想要一个只有FileVersion属性的对象,请使用select

PS D:\> (get-item c:\windows\system32\write.exe).VersionInfo | select FileVersion

FileVersion                                                                                                                                                                       
-----------                                                                                                                                                                       
6.1.7600.16385 (win7_rtm.090713-1255)                                                                                                                                             

如果您希望同一行上的属性名称和值使用format-list格式化输出:

PS D:\> (get-item c:\windows\system32\write.exe).VersionInfo | select FileVersion | fl


FileVersion : 6.1.7600.16385 (win7_rtm.090713-1255)

答案 1 :(得分:1)

fl是format-list的别名。在大多数情况下,应在输出结果之前使用格式命令。

如果您想选择特定属性,可以使用'。' -operator。在你的情况下:

(dir c:\windows\system32\dfc.exe).versioninfo.fileversion