how to filter name/value pairs under a registry key by name and value in PowerShell?

powershell, powershell-3.0, registry

Solution

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

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

Problem

I'm trying to get a feel for the idioms to use in PowerShell. Given this script: ``` $path = 'hkcu:\Software\Microsoft\Windows\CurrentVersion\Extensions' $key = Get-Item $path $key ``` I get the output at the bottom of this question. 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: - name like `xls*` - or value like `*\MSACCESS.EXE` 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 ``` This is the original output of the script: ``` 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 ``` Edit 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

Original source

Related problems