Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
Powershell 从字符串中提取版本号_Powershell - Fatal编程技术网

Powershell 从字符串中提取版本号

Powershell 从字符串中提取版本号,powershell,Powershell,使用以下PowerShell命令 Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion | Select-String 'Application Name' 我得到如下输出: @{DisplayName=应用程序名称;DisplayVersion=52.4.1521} 如果是

使用以下PowerShell命令

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
  Select-Object DisplayName, DisplayVersion |
  Select-String 'Application Name'
我得到如下输出:

@{DisplayName=应用程序名称;DisplayVersion=52.4.1521}

如果是在Unix上,我可能会想出一个
sed
awk
命令来提取版本号,但在Windows上我甚至不知道从哪里开始。如何将该版本号作为变量值输出?

get ChildItem
生成对象列表,因此您应该使用这些对象的属性。通过
Where Object
为具有您要查找的显示名称的对象筛选列表,然后展开
DisplayVersion
属性:

$regpath = 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$version = Get-ItemProperty "$regpath\*" |
           Where-Object { $_.DisplayName -eq 'Application Name' } |
           Select-Object -Expand DisplayVersion
您还可以让筛选器使用通配符进行部分匹配

... | Where-Object { $_.DisplayName -like '*partial name*' } | ...
或正则表达式

... | Where-Object { $_.DisplayName -match 'expression' } | ...

哇,太棒了。非常感谢。