How to delete registry key and values using Powershell

In my recent blog post, I discussed how to test if a registry key or registry entry and its value exist. This is helpful if you want to take certain actions based on the presence of a registry key or entry and its value.

In this blog post, I will show you how to easily delete a registry key or registry entries and values using Powershell script. Below are the highlights of this blog post:

I will use Remove-Item and Remove-ItemProperty PowerShell cmdlets are used to delete registry keys and entries. I also provided alternate commands to delete a registry entry at the end of this blog post.

Steps to delete a registry key using PowerShell script

Using a Powershell script, I will show you how to delete a registry key. Please find information about the script and its usage below.

The script will check the below condition [Example]:

  • If a registry key named cloudinfra.net exists under HKLM:\Software\

If the cloudinfra.net registry key exists, the script will delete it. If it does not exist, it will output Registry key HKLM:\Software\cloudinfra.net does not exist on the PowerShell console.

Modify the below variable in the script:

  • $regpath – Provide the registry key path you want to delete in this variable. For example, HKLM:\Software\cloudinfra.net.

Powershell Script to delete a Registry Key (regKeyDelete.ps1)

regKeyDelete.ps1

<#
.DESCRIPTION
    This script will delete registry key 
    specified in $regPath variable.
    Author: Jatin Makhija
    Website: cloudinfra.net
    Version: 1.0.0
#>
# Provide registry key path
$regPath = "HKLM:\Software\cloudinfra.net"
# Check if the registry key exists
if (Test-Path -Path $regPath) {
           try {
           Remove-Item -Path $regPath -force
           Write-Host "Registry key $regPath deleted."
         }
         Catch {
           Write-error $_
         }
}
         else {
          Write-Host "Registry key $regPath does not exist."
         }
Delete registry key using powershell script
Delete registry key using PowerShell script

Steps to delete a Registry Entry using Powershell

The screenshot below shows a registry key named cloudinfra.net containing registry entries for Location, Status, and Download with the respective United Kingdom, 1, and 123 values.

We will delete the registry entry labeled Status using a PowerShell script.

Example registry key cloudinfra.net and its registry entries
Example registry key cloudinfra.net and its registry entries

The script will check the two conditions below:

  • If a registry key named cloudinfra.net exists under HKLM:\Software\.
  • If a registry entry named Status as provided in the $valueName variable exists under HKLM:\Software\cloudinfra.net.

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

  • Registry key $regPath does not exist.
  • Registry value $valueName in $regPath 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 delete. For example: Status.

Powershell Script to Delete a Registry Entry

<#
.DESCRIPTION
    This script will Remove a registry entry specified in $regPath and $valueName
    variables. It will be deleted if it matches the type in $match variable.
    Author: Jatin Makhija
    Website: cloudinfra.net
    Version: 1.0.0
#>
# Provide registry key path
$regPath = "HKLM:\Software\cloudinfra.net"
# Provide registry entry name
$valueName = "Status"
# Check if the registry key exists
if (Test-Path -Path $regPath) {
    # Check if the registry value exists within the key
    $valueExists = Get-ItemProperty -Path $regPath -Name $valueName -ErrorAction SilentlyContinue
    if ($valueExists -ne $null) {
         try{
           Remove-ItemProperty -Path $regPath -Name $valueName
           Write-Host "Registry value $valueName in $regPath deleted."
         }
         Catch {
           Write-error $_
         }
}
         else {
          Write-Host "Registry value $valueName in $regPath does not exist."
         }
}
else {
Write-Host "Registry key $regPath does not exist."
}

The below screenshot shows the result after deleting a registry entry called Status:

Delete a registry entry called Status under HKLM:\Software\cloudinfra.net reg key
Delete a registry entry called Status under HKLM:\Software\cloudinfra.net reg key

Steps to delete registry entry based on its data type using Powershell

A specific requirement might be to delete a registry entry only when its display name and data type match certain criteria.

For example, you may want to delete the Status registry entry only if it has a String data type. It won’t be deleted if it has any other data type.

Example registry key cloudinfra.net and its registry entries
Example registry key cloudinfra.net and its registry entries

The script will check the three conditions below:

  • If a registry key named cloudinfra.net exists under HKLM:\Software\.
  • If a registry entry named Status as provided in the $valueName variable exists under HKLM:\Software\cloudinfra.net.
  • If the registry entry named Status matches the data type specified in the $match variable.

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

  • Registry key $regPath does not exist.
  • Registry value $valueName in $regPath does not exist.
  • Registry value $valueName in $regPath with type $existingvalueType 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 delete. For example: Status.
  • $match – Specify a data type of the registry entry to match; if it matches only, then deletion will occur.

Powershell script to Delete a registry entry with a specific data type

<#
.DESCRIPTION
    This script will Remove a registry entry specified in $regPath and $valueName
    variables. It will be deleted if it matches the type in $match variable.
    Author: Jatin Makhija
    Website: cloudinfra.net
    Version: 1.0.0
#>
# Provide registry key path
$regPath = "HKLM:\Software\cloudinfra.net"
# Provide registry entry name
$valueName = "Status"
# Provide the data type of registry entry to match
$match = "String"
# Check if the registry key exists
if (Test-Path -Path $regPath) {
    # Check if the registry value exists within the key
    $valueExists = Get-ItemProperty -Path $regPath -Name $valueName -ErrorAction SilentlyContinue
    if ($valueExists -ne $null) {
        # Get the data type of the registry value
        $existingvalueType = $valueExists.$valueName.GetType().Name
        # Delete the registry value based on its data type
        if($existingvalueType -eq $match) {
         try{
           Remove-ItemProperty -Path $regPath -Name $valueName
           Write-Host "Registry value $valueName in $regPath with type $existingvalueType deleted."
         }
         Catch {
           Write-error $_
         }
}
         else {
          Write-Host "Registry value $valueName in $regPath with type $existingvalueType does not exist."
         }
}
    else {
        Write-Host "Registry value $valueName in $regPath does not exist."
    }
}
else {
    Write-Host "Registry key $regPath does not exist."
}

The screenshot below shows that the script was executed successfully, deleting the Status registry entry under the HKLM:\Software\cloudinfra.net registry path.

This particular registry entry had a data type of String. Since the $match variable was set to String, it matched both the display name and data type values, leading to the removal of the registry entry.

Delete registry entry matching with specific data type using Powershell script
Delete registry entry matching with specific data type using Powershell script

How to delete a Registry Entry in the HKCU node using Powershell

HKCU is a short form of HKEY_CURRENT_USER. It contains the configuration information for the user who is currently logged on. The user’s folders, screen colors, and Control Panel settings are stored here. This information is associated with the user’s profile.

If you want to delete a registry entry from the HKCU node, update the variable $regPath and point it to the registry key in that node, for example, HKCU:\Software\cloudinfra.net. The rest of the script will remain the same.

An alternative way to delete a registry key/entry using Powershell

Remove-ItemProperty works well, but you can also use reg delete the command or Remove-Item PowerShell cmdlet to delete a registry key and registry entries.

1. Delete Registry key using reg delete

You can delete a registry key using the reg delete command below.

reg delete command to delete a registry key

reg delete "HKLM\Software\cloudinfra.net" /f
reg delete command to delete a registry key
reg delete command to delete a registry key

2. Delete a registry entry in a registry key using reg delete

You can use the command below to delete a specific registry entry using a registry key. For example, The below command will delete a registry entry called Status under HKLM\Software\cloudinfra.net registry key.

reg delete command to delete a registry entry

reg delete "HKLM\Software\cloudinfra.net" /v "Status" /f
reg delete command to delete a registry entry in a registry key
reg delete command to delete a registry entry in a registry key

3. Delete a registry key using the Remove-Item PowerShell cmdlet

There is another way to delete a registry key, which is by using Remove-Item PowerShell cmdlet. Below is an example command that will delete the cloudinfra.net registry key.

Remove-Item cmdlet to delete a Registry Key

Remove-Item -Path HKLM:\Software\cloudinfra.net -Force -Verbose
Remove-Item PowerShell cmdlet to delete a registry key

Conclusion

In this blog post, I’ve shared PowerShell scripts that allow you to delete a specific registry entry. The first script focuses solely on checking the display name and removing the registry entry without considering its data type.

The second script verifies the display name and data type values before deleting the registry entry. Additionally, we explored alternative methods for removing registry entries, such as using the “reg delete” command and the Remove-Item PowerShell cmdlet.

Leave a Comment