Posted On September 3, 2020

PowerShell: Check Registry Path, Key, and Dword Value

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

WordPress Code Snippet Crashed My Site

Error Message: ParseError thrown syntax error, unexpected '$', expecting variable (T_VARIABLE) Resolution:1. If still login…

PowerShell: Running Commands on Remote Computers

# runCommandsOnRemoteComputers.ps1 # User defined variables $computernames=@( 'SERVER001', 'SERVER002' ) $expectedExecutable='racadm.exe' $expectedInstallPath='C:\program files\Dell\SysMgt\iDRACTools\racadm' # Execution…

Basic JavaScript: Manipulate Arrays With push()

// Setupvar myArray = [["John", 23], ["cat", 2]];// Add a dog... ezmyArray.push(["dog",3]);