PowerShell to Test If Registry key and Value exists

When creating a PowerShell script, you might need to verify whether a specific registry key or registry entry and its value exists and then continue with the script based on the outcome. There are multiple ways to check this. I will be using the Test-Path PowerShell cmdlet. It will check if a given path exists or not and provide the result in a boolean form, which would be either $true or $false.

In addition to the Test-Path cmdlet, I will be using the Get-ItemProperty PowerShell cmdlet to retrieve registry keys and their values.

There are other alternative commands you can use to fetch registry keys and values, such as Get-Item or reg query. I’ve covered these alternate commands and provided examples with screenshots at the end of this blog post.

Test If the Registry Key Exists using PowerShell

As an example, I will test the existence of a registry key named cloudinfra.net. The below script will not check the registry entry or its value, It will just check if a registry key name cloudinfra.net exists under HKLM:\Software\ registry path.

  • $regpath – Provide registry key path in this variable
  • $value – This will be either True or False, depending on whether the Key exists.
  • If the $value is True, it will output Reg Key already Exists. No Action Required. Else, It will output Reg Key does not exist

Script to Test for Registry Key Existence

$regPath = "HKLM:\Software\cloudinfra.net"
$value = Test-Path $regPath
if($value){
        Write-host "Reg Key already Exists. No Action Required"
 }
Else{
        Write-host "Reg Key does not exist"
}
Example Registry Key cloudinfra.net
Example Registry Key cloudinfra.net

Check for the Existence of a Registry Key, Entry, and Value Using PowerShell.

In the previous section, I demonstrated how to verify the existence of a registry key named cloudinfra.net. It does not check the registry entries and its values. If you want to check and confirm whether Status registry entry exists and its value is set correctly, then you can use the following code.

This script will check three things:

  • If a registry key named cloudinfra.net exists under HKLM:\Software.
  • If a registry entry named Status exists under HKLM:\Software\cloudinfra.net registry key
  • If a registry entry named Status is set to a value of 1

If any of the above conditions is not true, then the script will output one of the following messages on the console:

  • Reg value exists but does not match the required value.
  • Registry value does not exist.
  • Registry key does not exist.

A few points about the script:

  • $regpath: Provide a registry key path for this variable. For example: HKLM:\Software\cloudinfra.net.
  • $valueName: Provide the display name of the registry entry you want to check. For example: Status.
  • $requiredValue: This is the value at which you expect the Status registry entry to be set. For example, you want to determine if the registry entry name Status value is set to 1. If it’s set to something other than 1, it will output a Reg value exists but does not match the required value.

Script to Test the Existence of the Registry Entry and its Value

<#
.DESCRIPTION
    This script will test existence of registry key, registry entry and
    its value to match with required value and provide the result 
    Author: Jatin Makhija
    Website: cloudinfra.net
    Version: 1.0.0
#>
#Provide registry key path 
$regPath = "HKLM:\Software\cloudinfra.net"
#Provide registry entry display name 
$valueName = "Status"
#Provide registry entry expected value 
$requiredValue = "1"
$regkeyexists = Test-Path -Path $regPath
if ($regkeyexists) {
   #Check if registry entry named Status exists
   $regentryexists = Get-ItemProperty -Path $regpath -Name Status -ErrorAction SilentlyContinue
   if ($regentryexists) {
   #If registry entry named Status exists, then fetch its value
    $currentValue = Get-ItemProperty -Path $regpath | Select-Object -ExpandProperty $valueName -ErrorAction SilentlyContinue
    #Match Status registry entry value with requied value
    if ($currentValue -eq $requiredvalue) {
            Write-Host "Reg value exists and matching the required value."
        } else {
            Write-Host "Reg value exists, but does not match the required value."
            Write-Host "Current value: $currentValue"
            Write-Host "Required value: $requiredValue"
        }
    } 
    else {
        Write-Host "Registry value does not exist."
    }
} 
else {
    Write-Host "Registry key does not exist."
}

Another Way to Get Registry Key and Values using PowerShell

There are alternative methods for retrieving registry keys, registry entries, and their values from registry using PowerShell. Let’s take a look:

1. Use Get-ItemProperty to fetch Registry Entries and its Values

Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\Software\cloudinfra.net
Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\Software\cloudinfra.net
Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\Software\cloudinfra.net

2. Use Get-Item to fetch Registry Entries

Get-Item -Path Registry::HKEY_LOCAL_MACHINE\Software\cloudinfra.net | Select -expandproperty Property
Get-Item -Path Registry::HKEY_LOCAL_MACHINE\Software\cloudinfra.net | Select -expandproperty Property
Get-Item -Path Registry::HKEY_LOCAL_MACHINE\Software\cloudinfra.net | Select -expandproperty Property

3. Use reg query to get Registry Entries and its Values

reg query HKLM\Software\cloudinfra.net
reg query HKLM\Software\cloudinfra.net
reg query HKLM\Software\cloudinfra.net

4. Use reg query to get specific Registry Entries and their Values

reg query HKLM\Software\cloudinfra.net /v Status
reg query HKLM\Software\cloudinfra.net /v Status
reg query HKLM\Software\cloudinfra.net /v Status

Conclusion

In this blog post, we’ve covered how to determine the existence of a registry key. Additionally, we’ve explored testing and confirming the presence of registry entries and their values within a specific registry key.

1 thought on “PowerShell to Test If Registry key and Value exists”

  1. This script has been so helpful, thank you so much. One thing that I would consider changing that I had trouble with is this line
    $regentryexists = Get-ItemProperty -Path $regpath -Name Status -ErrorAction SilentlyContinue

    I changed “Status” to $valueName

    Reply

Leave a Comment