如何在PowerShell中按名称和值过滤注册表项下的名称/值对? [英] how to filter name/value pairs under a registry key by name and value in PowerShell?

查看:48
本文介绍了如何在PowerShell中按名称和值过滤注册表项下的名称/值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解在 PowerShell 中使用的习语.

I'm trying to get a feel for the idioms to use in PowerShell.

鉴于此脚本:

$path = 'hkcu:\Software\Microsoft\Windows\CurrentVersion\Extensions'
$key = Get-Item $path
$key

我在这个问题的底部得到了输出.

I get the output at the bottom of this question.

我想获取属性的输出($key 下的名称/值对),我可以在其中过滤名称和值.

I'd like to get output of the properties (the name/value pairs under the $key) where I can filter on both name and value.

例如,过滤以列出所有具有以下功能的扩展:

For instance, filter to list all the Extensions that have:

  • 名称如xls*
  • 或像 *\MSACCESS.EXE
  • 这样的值

或排除过滤器:排除所有名称,如 doc*

Or an exclude filter: exclude all names like doc*

第一个过滤器,我想要这样的结果:

Fir the first filter, I'd want a result like this:

Name                           Value                                                                                                                                                       
----                           --------                                                                                                                                                       
xlsx                           C:\PROGRA~2\MICROS~1\Office15\EXCEL.EXE                                                                                                                 
xls                            C:\PROGRA~2\MICROS~1\Office15\EXCEL.EXE                                                                                                                 
mdb                            C:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE                                                                                                              
mda                            C:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE                                                                                                              

这是脚本的原始输出:

Hive: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion
Name                           Property                                                                                                                                                       
----                           --------                                                                                                                                                       
Extensions                     rtf  : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.rtf                                                                                                         
                               dot  : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.dot                                                                                                         
                               dotm : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.dotm                                                                                                        
                               dotx : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.dotx                                                                                                        
                               docm : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.docm                                                                                                        
                               docx : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.docx                                                                                                        
                               doc  : C:\PROGRA~2\MICROS~1\Office15\WINWORD.EXE ^.doc                                                                                                         
                               xlsx : C:\PROGRA~2\MICROS~1\Office15\EXCEL.EXE                                                                                                                 
                               xls  : C:\PROGRA~2\MICROS~1\Office15\EXCEL.EXE                                                                                                                 
                               mdb  : C:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE                                                                                                              
                               mda  : C:\PROGRA~2\MICROS~1\Office15\MSACCESS.EXE                                                                                                              

编辑

我解决了部分问题:获取名称/值对的列表.它使用 PSCustomObject:

I solved part of the problem: getting a list of Name/Value pairs. It uses PSCustomObject:

$namevalues = $key.GetValueNames() | ForEach-Object { [pscustomobject]@{ Name=$_; Value=$key.GetValue($_) } }
$namevalues

(我应该如何包装该代码?)

(How should I wrap that code?)

对过滤的任何帮助将不胜感激

Any help with the filtering would be much appreciated

推荐答案

有一个更聪明的方法来枚举注册表值(发现它 此处).而且它更像是 IMO 的 powershell 方式.

There's a much more clever way to enumerate registry values (found it here). And it's more powershell-way IMO.

我已经把它变成了单线:

I've turned it into a one-liner:

(Get-ItemProperty $path).psobject.properties | 
   where {$_.name -like "xls*" -or $_.value -like "*\MSACCESS.EXE"} | 
      select name,value

更新:正如@mklement0 在评论中所指出的,您应该注意属性PSPathPSParentPathPSChildNamepsobject.properties 中的 code>、PSDrivePSProvider.

Update: As noted in comments by @mklement0, you should be careful about properties PSPath, PSParentPath, PSChildName, PSDrive, and PSProvider within the psobject.properties.

这篇关于如何在PowerShell中按名称和值过滤注册表项下的名称/值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆