functions/Test-RegistryItem.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<#
    .SYNOPSIS
    Tests if the registry entry at the specified path exists and has the specified value.
 
    .DESCRIPTION
    This CMDLet tests if the registry value at the specified path exists, and is of an expected value.
 
    .EXAMPLE
    Test-RegistryItem -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management' -Name 'FeatureSettingsOverride' -ExpectedValue 0
    Will check if the registry value FeatureSettingsOverride found at HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management has a value of 0
#>

function Test-RegistryItem
{
    [CMDLetBinding()]
    [OutputType([System.Boolean])]
    Param (
        # Specifies the path of the entry. This parameter identifies the entry to which this cmdlet sets or creates the new property.
        [Parameter(Mandatory = $true)]
        [ValidateNotNullorEmpty()]
        [String]
        $Path,

        # Specifies a name for the entry.
        [Parameter(Mandatory = $true)]
        [ValidateNotNullorEmpty()]
        [String]
        $Name,

        # Specifies the expected property value of the entry.
        [Parameter(Mandatory = $true)]
        [ValidateNotNullorEmpty()]
        [String]
        $ExpectedValue
    )

    # Test if the path exists
    if (-not (Test-Path -Path $Path)) {
        return $false
    }

    $Item = Get-Item -Path $Path

    # Tests if the item exists
    if (-not ($Item.Property.Contains($Name))) {
        return $false
    }

    # Test if the value matches
    return ($Item.getValue($Name) -eq $ExpectedValue)
}