如何使用PowerShell获取键内注册表值的数据值

时间:2017-04-19 14:55:17

标签: powershell

在我的函数中,我想在特定子项的数据部分中搜索特定字符串。函数内部的一部分代码如下:

 foreach ($CurrentPath in $Path) { 
    $Items = Get-Item -Path Registry::$CurrentPath
    ForEach ( $Property in $Items) {
      $Key = $Property

当我在$Key = $Property之后直接调试并转到主机命令窗口并输入$Key并按Enter键。它返回:

Hive: hkcu

Name                           Property
----                           --------
ABC                            Test : ababab\MSSQLSERVER_2014\ababab

我希望我的功能也可以搜索数据部分,如附在图片中所示,这是在注册表中。

Regedit image sample

如何完成搜索?

1 个答案:

答案 0 :(得分:1)

如果您不知道值名称,则需要循环属性。使用Get-Item的示例:

$item = Get-Item -Path "Registry::HKEY_CURRENT_USER\Software\NuGet"

foreach ($prop in $item.Property) {
    if($item.GetValue($prop) -match '0') { "Match found in key $($item.PSPath) , value $($prop)" }
}

Get-ItemProperty

$item = Get-ItemProperty -Path "Registry::HKEY_CURRENT_USER\Software\NuGet"

foreach ($prop in $item.psobject.Properties) {
    if($prop.Value -match '0') { "Match found in key $($item.PSPath) , value $($prop.Name)" }   
}

如果你知道价值名称,那么使用Get-ItemPropertyWhere-Object就更容易,例如:

Get-ItemProperty -Path "Registry::HKEY_CURRENT_USER\Software\NuGet" | Where-Object { $_.IncludePrerelease -match '0' }