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
{

<#

.Synopsis
 Validates the existence of a registry key
.Description
 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
.Example
Test-PathReg -Path HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters -Property Hostname
.Link
 https://blogs.msdn.com/candede
.Notes
 Author: Can Dedeoglu
#>

param
(
[Parameter(mandatory=$true,position=0)]
[string]$Path
,
[Parameter(mandatory=$true,position=1)]
[string]$Property
)

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

Cheers, CanD

Test-PathReg.ps1