PowerShell: Check Registry Path, Key, and Dword Value

$path='HKLM:\Software\Intel'
$keyName='GMM'
$dwordName='DedicatedSegmentSize'
$dwordValue=512

Function CheckRegistryKey {
    param(
        [Parameter(Position = 0, Mandatory = $true)][String]$path,
        [Parameter(Position = 1, Mandatory = $true)][String]$keyName,
        [Parameter(Position = 2, Mandatory = $false)]$dwordName,
        [Parameter(Position = 3, Mandatory = $false)]$dwordValue
    ) 
    try{
        $keyPath="$path\$keyName"
        $keyExists=gi -Path $keyPath -ea Stop
        $message=''
        if($keyExists){
            $message+="$keyPath is valid`r`n"
            if($dwordValue -and $dwordName){
                $dwordValueExists=$keyExists.getvalue($dwordName)
                if($dwordValueExists -ne $dwordValue){
                    $message+="$dwordValueExists does not match $dwordValue."
                    $result=$false
                }else{
                    $message+="Existing dword value $dwordValueExists matches the desired value of $dwordValue."
                    $result=$true
                    }
            }else{
                $result=$true
                }
        }else{
            $message+="$keyPath is invalid"
            }
        write-host $message
        return $result
    }catch{
        Write-Error $_
        return $false
        }
}

CheckRegistryKey $path $keyName $dwordName $dwordValue

Leave a Reply

Your email address will not be published. Required fields are marked *