When you are creating a powershell script, you may have a requirement to check and confirm if a particular registry key or registry entry and its value is existing and proceed further on powershell script based on the result.
There are multiple ways to test if a registry key or registry entries and its value is existing. I will be using Test-Path
powershell cmdlet. It will check if a given path exists or not and provide the result in 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.
Along with Test-Path cmdlet, I will be using Get-ItemProperty
powershell cmdlet to fetch registry key and its values. You can also use other commands to fetch registry keys and values like Get-Item
or reg query
. I have discussed about these commands and provided examples with screenshots at the end of this blog post. Let’s start with testing the existence of a registry key using powershell.
Test if registry key exists using powershell
For example: I will test the existence of a registry key named cloudinfra.net. Below script will not check registry entry or its value, It will just check if a folder/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 upon if the Key exists or not.
- If $value is True then it will output “Reg Key already Exists. No Action Required“
- Else it will output “Reg Key does not exist“
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" }
You can find below elements in the screenshot:
- Registry Key – cloudinfra.net
- Registry Entries – There are two registry entries which are existing apart from Default one. First one is Location and second one is Status. Their value’s are set to United Kingdom and 1 respectively.
Test If registry key, registry entry and its value exists using Powershell
In the previous section, I have tested to confirm if registry key named cloudinfra.net exists or not. If you want to check it further to find out if registry entry named Status exists and its value is set as we want then you can use below snippet of 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 value 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.
Few points about the script:
- $regpath – Provide registry key path in this variable. for example: HKLM:\Software\cloudinfra.net.
- $valueName – Provide the display Name of registry entry which you want to check. For example: Status.
- $requiredValue – This is the value you expect Status registry entry will be set to. For example: You want to find out if registry entry name Status value is set to 1. If it’s set to other than 1 it will result “Reg value exists, but does not match the required value.”
Test existence of 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 ways to fetch registry keys / registry entries and its values from Windows registry using powershell. I have provided the commands with examples and screenshots in following sections of the blog post.
Use Get-ItemProperty to fetch registry entries and its values
Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\Software\cloudinfra.net
Use Get-Item to fetch Registry Entries
Get-Item -Path Registry::HKEY_LOCAL_MACHINE\Software\cloudinfra.net | Select -expandproperty Property
Use reg query to get registry entries and its values
reg query HKLM\Software\cloudinfra.net
Use reg query to get specific registry entries and its values
reg query HKLM\Software\cloudinfra.net /v Status
Conclusion
In this blog post, we have discussed how to test the presence of a registry key. Then we have tested and checked the presence of registry entries and its value in a particular registry key. There could be several reasons for testing the presence of a registry key, it could be for Intune device remediations powershell script or could be required to test a certain configuration on Windows 10/11 devices via registry.