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 to delete registry keys and entries. There are more ways to delete a registry key and entries which are discussed at the end of this blog post.

Delete a Registry Key Using PowerShell script

Let’s check the steps to delete a registry key using a powershell script. As an example, I have already created a registry key called cloudinfra.net, the script will first check if this key is existing or not. If it exists, the script will delete it. If it does not exist, the script will output Registry key HKLM:\Software\cloudinfra.net does not exist.

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

delete_reg_key.ps1

# 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

Delete a Registry Entry using Powershell script

Registry entries exist under the registry key which may have the type of string, Dword etc. Below screenshot shows a registry key 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 below two conditions:
    • 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.

delete_reg_entry.ps1

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

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

Delete a registry entry using Powershell script
Delete a registry entry using Powershell script

Delete a 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.

delete_reg_entry_type_condition.ps1

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

Below screenshot shows that the script is 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

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. For more help on modifying registry keys and entries in HKCU registry node using powershell script, refer to the post: Create HKCU Registry Keys using Intune remediations.

Alternative Ways 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 Using reg delete

You can use below command to delete a specific registry entry. For example, this 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

Another way to delete a registry key 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 will delete a specific registry key/entry. First script focuses solely on checking the display name and removing the registry entry without considering its data type.

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