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 exist and then continue with the script based on the outcome.

There are multiple ways to test if a registry key or registry entry and its value exist. 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 a recent blog post, I have discussed on How To Delete Registry Key And Values Using Powershell along with deleting a registry entry based on its data type. Also, I have discussed about alternative methods to delete registry entry using powershell.

Step-by-Step guide

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. Now, let’s begin by testing the existence of a registry key using PowerShell.

Test If the Registry Key Exists using Powershell

For 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 If you wish to inspect further whether the registry entry named Status exists and whether its value is correctly set, you can utilize the following code snippet.

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."
}

Alternate way to get registry key and values using Powershell

There are alternative methods for retrieving registry keys, registry entries, and their values from the Windows registry using PowerShell. I’ve detailed these commands along with examples and screenshots in the subsequent blog post sections.

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.

There are various reasons for performing these tests, whether for Intune device remediation PowerShell scripts or to assess specific configurations on Windows 10/11 devices via the registry.

Leave a Comment