Get-ChildItem and Registry Key Access Not Allowed

I am trying to make a simple PowerShell script to access the registry, and I am doing this as follows:

Foreach ($key in Get-Childitem HKLM:\SYSTEM\CurrentControlSet\Control\Class\"{4D36E972-E325-11CE-BFC1-08002BE10318}") {
    $key.name
}

There are a bunch of keys that are just numbers (the ones I want), but then where there is a name “Properties” that I don’t have access to (I don’t need it), and this key gives me the following error executing the command Foreach:

Foreach ($key in Get-Childitem HKLM:\SYSTEM\CurrentControlSet\Control\Class\"{4D36E972-E325-11CE-BFC1-08002BE10318}") {
    $key.name
}
Get-ChildItem : Requested registry access is not allowed.
At line:3 char:31
+ Foreach ($key in Get-Childitem <<<<  HKLM:\SYSTEM\CurrentControlSet\Control\Class\"{4D36E972-E325-11CE-BFC1-08002BE10318}") {
    + CategoryInfo          : PermissionDenied: (HKEY_LOCAL_MACH...318}\Properties:String) [Get-ChildItem], SecurityException
    + FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.PowerShell.Commands.GetChildItemCommand

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0000
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0001
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0002
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0003
(...)
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0024

How can I exclude the "Properties" key and get rid of this error?

+3
source share
1 answer

If you just don’t want to “see” the error, use -ErrorActionGet-ChildItem, for example:

$path = "HKLM:\SYSTEM\CurrentControlSet\Control\Class\" +
        "{4D36E972-E325-11CE-BFC1-08002BE10318}"
Get-Childitem $path -ErrorAction SilentlyContinue | Foreach {$_.Name}

SilentlyContinue PowerShell . , PowerShell , perms .

+5

Source: https://habr.com/ru/post/1770925/


All Articles