In my recent blog post, I have discussed about how to test if a registry key or registry entry and its value exists. This is helpful if you want to take certain action based on the presence of a registry key or to take certain action based on the presence of a registry entry and its value.
In this blog post, I will show you how you can easily delete a registry key or registry entries and values using Powershell script. Below are the highlights of this blog post:
- How to delete specific registry key using Powershell script. – Delete a specified registry key.
- How to delete specific registry entry using Powershell script – It will delete the registry entry based on its display name and irrespective of its type e.g. String, DWORD or binary etc.
- How to delete specific registry entry with specific data type using Powershell script. – Script will check for the data type of registry entry along with its Display name. If both conditions are true then registry entry will be deleted.
I will be using, Remove-Item
and Remove-ItemProperty
powershell cmdlets to delete a registry key and registry entries. At the end of this blog post, I have provided alternate commands to delete a registry entry.
Steps to delete registry key using powershell script
Now, I will show you how to delete a registry key using a powershell script. Please find below information about the script and its usage.
The script will check below condition:
- If a registry key named cloudinfra.net exists under HKLM:\Software\
If cloudinfra.net registry key exists, script will delete it and If it does not exists then it will output “Registry key HKLM:\Software\cloudinfra.net does not exist.” on powershell console.
Modify below variable in the script:
- $regpath – Provide registry key path which you want to delete in this variable. For example: HKLM:\Software\cloudinfra.net.
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." }
Steps to delete registry entry using Powershell
As you can see in below screenshot, I have a registry key name cloudinfra.net with registry entries Location, Status and Download with values United Kingdom, 1 and 123 respectively. I will delete a registry entry called Status using a powershell script.
The script will check below two conditions:
- If a registry key named cloudinfra.net exists under HKLM:\Software\
- If a registry entry named Status as provided in $valueName variable is existing 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.
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 delete. For example: Status.
Delete a registry entry using below Powershell script
<# .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:
Steps to delete registry entry based on its data type using Powershell
You could have a requirement to delete a registry entry only when its Display Name and data type both are matching. For example: I want to delete Status registry entry only if its of String data type. If it find’s Status registry entry of any other data type than String, it will not be deleted.
The script will check below three conditions:
- If a registry key named cloudinfra.net exists under HKLM:\Software\
- If a registry entry named Status as provided in $valueName variable is existing under HKLM:\Software\cloudinfra.net
- If registry entry named Status is matching with the data type as specified in $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.
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 delete. For example: Status.
- $match – Specify a data type of the registry entry to match, If it matches only then deletion will occur.
Delete a registry entry with specific data type using below Powershell script
<#
.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."
}
As you can seen from below screenshot, The script is executed successfully and deleted Status registry entry under HKLM:\Software\cloudinfra.net registry path which was of data type String. $match variable is set to “String“. Therefore, both the display name and data type values matched and registry entry is removed.
How to delete a registry entry in 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 HKCU node then update the variable $regPath and point it to the registry key in that node. For example: “HKCU:\Software\cloudinfra.net“. Rest of the script will remain the same.
Alternative way to delete a registry key / entry using Powershell
There are alternative ways to delete a registry entry using Powershell. Remove-ItemProperty
works really well, but you can also use reg delete
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 below reg delete command.
reg delete command to delete a registry key
reg delete "HKLM\Software\cloudinfra.net" /f
2. Delete a registry entry in a registry key using reg delete
If you want to delete a specific registry entry in a registry key the you can use below command. For example: 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
3. Delete a registry key using 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 which will delete cloudinfra.net registry key.
Remove-Item cmdlet to delete registry key
Remove-Item -Path HKLM:\Software\cloudinfra.net -Force -Verbose
Conclusion
In this blog post, I have provided powershell scripts which will delete a given registry entry. First script will not check for registry entry data type, it only checks its display name and deletes a registry entry. Second script will check registry entry display name and data type values, if both match then only it will delete the registry entry. We also discussed on alternative ways to delete a registry entries, for example: Using reg delete
command and Remove-Item
powershell cmdlet.
READ NEXT
- Powershell To Test If Registry Key And Value Exists
- Retrieve Powershell Scripts Deployed Via Intune
- Windows Powershell Launching And Closing Abruptly
- How To Add A Group Tag To Autopilot Devices In Intune Using Powershell
- Microsoft Intune Powershell Approval Required. The App Requires Your Admin Approval