0

I am trying to find the path in Registry of a hardware device, which requires iterating over multiple Registry keys. I use break to exit the foreach loop after the first match is found, but for some reason, the break statement causes the outer if statement to exit without executing.

${device} = (Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "HID Keyboard%"');
if ( ${device} -ne ${null} ) {
    ${driver_key} = (${device}.GetDeviceProperties().DeviceProperties |
        Select-Object -Property data |
        Select-Object -Skip 6 -First 1).data;

    Get-ChildItem -Path "HKLM:\SYSTEM\ControlSet001\Enum" -Recurse -ErrorAction SilentlyContinue |
        foreach {
            if ( ${_}.GetValue("Driver") -eq "${driver_key}" ) {
                ${path} = ${_}.PSPath;
                Write-Output "Found!";
                break;
            }
        }

    Write-Output "${path}"; # this does not run
}

If I comment out the break statement, it will work. But that mean the foreach loop continues to iterate after a match is found. What am I doing wrong?

By the way, what does the percentage sign in HID Keyboard% means? I usually use * instead.

3
  • 1
    The break statement breaks you out of the if statement as well as the foreach loop. To get your desired output, you can use a return statement inside a ForEach-Object loop instead of a foreach loop. Something like: ForEach-Object {if ( $_.GetValue("Driver") -eq "${driver_key}" ) {Write-Output "Found!" return $_.PSPath}} Commented Jan 26, 2024 at 8:58
  • 1
    … | foreach { …. } is actually running | foreach-object { … }, not the similarly named foreach( $x in $y) { … }, and break inside a foreach-object terminates the next foreachor switch up the call stack (or the entire script if none) - learn.microsoft.com/en-us/powershell/module/… and stackoverflow.com/a/10420522/3156906. If you want to break a loop use foreach( $x in $y ) { … } rather than foreach-object. Commented Jan 26, 2024 at 9:12
  • 1
    See also - stackoverflow.com/questions/77743410/… - similar question with same underlying issue but slightly different scenario. Commented Jan 26, 2024 at 9:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.