Public/Test-specRegistryValueData.ps1

function Test-specRegistryValueData {
    <#
    .SYNOPSIS
        Tests if a registry value exists and if its data matches the expected data.
 
    .DESCRIPTION
        The Test-specRegistryValueData function checks whether a specified registry value exists within a given registry key path and compares its data against the expected data.
 
    .PARAMETER RegistryKeyPath
        The path to the registry key where the value is located.
 
    .PARAMETER ValueName
        The name of the registry value to check.
 
    .PARAMETER ExpectedData
        The expected data of the registry value to compare against.
 
    .EXAMPLE
        Test-specRegistryValueData -RegistryKeyPath "HKCU:\Software\YourSoftware" -ValueName "YourValueName" -ExpectedData "ExpectedValue"
        Tests if the registry value 'YourValueName' in the key 'HKCU:\Software\YourSoftware' exists and has the data 'ExpectedValue'.
 
    .EXAMPLE
        $result = Test-specRegistryValueData -RegistryKeyPath "HKLM:\SOFTWARE\WOW6432Node\Specsavers\info" -ValueName "DeviceType" -ExpectedData "Dispense"
        if ($result) {
            Write-Output "The registry value exists and contains the expected data."
        } else {
            Write-Output "The registry value does not exist or does not contain the expected data."
        }
        Tests if the registry value 'DeviceType' in the key 'HKLM:\SOFTWARE\WOW6432Node\Specsavers\info' exists and has the data "Dispense". Outputs a message based on the result.
 
    .NOTES
        Author: owen.heaume
        Version: 1.0.0
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$RegistryKeyPath,

        [Parameter(Mandatory = $true)]
        [string]$ValueName,

        [Parameter(Mandatory = $true)]
        [object]$ExpectedData
    )

    try {
        $regValue = Get-ItemProperty -Path $RegistryKeyPath -Name $ValueName -ErrorAction Stop
        if ($regValue.$ValueName -eq $ExpectedData) {
            Write-Verbose "The registry value '$ValueName' in the key '$RegistryKeyPath' exists and contains the expected data."
            return $true
        } else {
            Write-Verbose "The registry value '$ValueName' in the key '$RegistryKeyPath' exists but does not contain the expected data."
            return $false
        }
    } catch {
        Write-Verbose "The registry value '$ValueName' in the key '$RegistryKeyPath' does not exist."
        return $false
    }
}