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

PowerShell: How to Logoff an User RDP Session

# logOffUser.ps1 $servername=$env:computername $userName='brucelee' function logOffRdpSession{ param( $serverName=$env:computername, $username ) $username=if($username -match '\\'){[regex]::match($username,'\\(.*)$').captures.groups.value[1] }else{ $username.tostring()…

PowerShell: Raise Domain Forest Functional Level

# Raise Forest Functional Level$forest=Get-ADForest# 2012R2 LevelSet-ADForestMode -Identity $forest -Server $forest.SchemaMaster -ForestMode Windows2012R2Forest -Force# 2016…

PowerShell: Set Enhanced Protected Mode of Internet Explorer

function changeIeProtectedMode{ # $hives = 0..4|%{"HKLM:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\$_"} $hives = 0..4|%{"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\$_"} $keyName='2500' # Key Name…