Home ยป How to Check If Registry Key Exists in PowerShell

How to Check If Registry Key Exists in PowerShell

The Test-Path cmdlet in PowerShell is used to check if the registry key exists at the specified path.

The following method can be used to check if a registry key exists.

Method 1: Check if a registry key exists in PowerShell

$registryPath = "HKLM:\Software\SysApp"

if(Test-Path $registryPath) { 
   # key exists
} else {
   #key does not exists
}

In this example, the Test-Path cmdlet in PowerShell checks for the registry key at the specified path $registryPath and returns True or False based on whether the key exists.

The following example shows how to use this method.

How to Check If Registry Key Exists in PowerShell

To check if a registry key exists in PowerShell, you can use the Test-Path cmdlet. This command checks if an item exists at the specified path, including the registry keys.

The following example shows how to do it with syntax.

$registryPath = "HKLM:\Software\SysApp"

if(Test-Path $registryPath){
    Write-Output "Registry key exists"
} else {
    Write-Output "Registry key doesn't exists"
}

Output:

Registry key exists

In this PowerShell script, the $registryPath variable holds the path of the registry key that you want to check for existence.

The Test-Path cmdlet in PowerShell checks if the registry key at the specified location $registryPath exists. If it exists, the Write-Output cmdlet prints “Registry key exists” to the console; otherwise, it prints “Registry key doesn’t exist“.

Conclusion

I hope the above article on checking if the registry key exists in PowerShell using the Test-Path cmdlet is helpful to you.