Search code examples
powershellregistry

Powershell - Searching value for 32 and 64 bits in registry


I currently working on powershell script who collect some datas into registry.

To perform these action, I use a similar code:

$Ifkeyexist = Test-Path 'HKLM:\SOFTWARE\WOW6432Node\XXXX\YYY\ZZZ\environment\'
if ($Ifkeyexist -eq "True")
{
$GetProductHotfix = Get-ItemPropertyValue 'HKLM:\SOFTWARE\WOW6432Node\XXXX\YYY\ZZZ\environment\' 'ProductHotfix'
Write-host "- Product Hotfix: $GetProductHotfix"
}
else {
write-host "- Unable to find product hotfix" -ForegroundColor red
}
} 

Question: in the example above, is it possible to find the value "ProductHotfix" in the 32 and 64 bit registry path?

Thanks by advance for your advices :)

Regards,

LEFBE


Solution

  • I would do something like below:

    foreach ($path in 'HKLM:\SOFTWARE\XXXX\YYY\ZZZ\environment', 'HKLM:\SOFTWARE\WOW6432Node\XXXX\YYY\ZZZ\environment') {
        $hotfix = Get-ItemPropertyValue -Path $path  -Name 'ProductHotfix' -ErrorAction SilentlyContinue
        # assuming you want to exit the loop at the first successfull 'hit'
        if ($hotfix) { break }
    }
    
    if ($hotfix) {
        Write-Host "- Product Hotfix: $hotfix"
    }
    else {
        Write-Host "- Unable to find product hotfix" -ForegroundColor Red
    }