Test Registry Paths

Test-Path is a great cmdlet to verify the existence of files and folders but unfortunately it doesnt verify the existence of Registry keys which Test-PathReg does. Test-PathReg works pretty much same as Test-Path; return true/false as a result.

If you want to check if Hostname property exists under HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters; run the function like this:

Test-PathReg HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters Hostname

Here is the function code; as usual check the attachment to get it in ps1 format

function Test-PathReg


 Validates the existence of a registry key
 This function searches for the registry value (Property attribute) under the given registry key (Path attribute) and returns $true if it exists
.Parameter Path
 Specifies the Registry path
.Parameter Property
 Specifies the name of the registry property that will be searched for under the given Registry path
Test-PathReg -Path HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters -Property Hostname
 Author: Can Dedeoglu


$compare = (Get-ItemProperty -LiteralPath $Path).psbase.members | %{$_.name} | compare $Property -IncludeEqual -ExcludeDifferent
if($compare.SideIndicator -like "==")
        return $true
        return $false

Cheers, CanD


Skip to main content