Reputation: 48
I am trying to get all registry keys beginning with a digit 1-9. This is what I am working with:
Get-ChildItem -Path HKLM:\SOFTWARE\Policies\Citrix | Where-Object {$_.name -Match '^[1-9]'}
Simple huh? Should work? Doesn't work! What am I missing?
Update1:
$CitrixPolRegDel = (Get-ChildItem -Path $CitrixPolRegPath).pschildName | Where-Object {$_ -Match '^[0-9]{1,2}$'}
This is what I am using now, pschildname turned out to be easier to use than the split example given below, but there might be drawbacks to this method that I am not aware of?
I also updated the regex expression to match regkeys named with one or two digits:
Regex retreives regkeys where name starts with (^
) a digit 0-9 ([0-9]
), digit in name can occur one to two times ({1,2}
) and then the name should end ($
)
Upvotes: 1
Views: 2891
Reputation: 36322
So, the funny thing is that you run that GCI and it kicks back a table with the name of the sub keys and that's it, but if you do a Select -Expand Name it gives the full HKLM\Software\Policies\Citrix. So there's the problem, Name actually contains the full name of the key, not just the last sub-key.
So, you want the subkeys that start with 1-9? Split it out by \
and just use the last one for each entry. Try this out:
(Get-ChildItem -Path HKLM:\SOFTWARE\Policies\Citrix).Name | ForEach {$_.Split("\")[4] | Where-Object {$_.name -Match '^[1-9]'}}
Upvotes: 1