Uncategorized

Use PowerShell to Search for and Delete Registry Values

This post has nothing to do with Intune or Modern Management directly but hopefully is still useful to someone.

Working with the registry via PowerShell is a bit of a pain in my experience. The worst part is that we all know how to add/remove/change values via regedit so it feels like any issues we experience when using PowerShell are trivial. I’ve just spent a couple of hours scratching my head with this challenge and wanted to ensure this didnt happen again or to someone else…so I’m blogging it here if only for my own personal reference.

My objective was to simply remove a registry value – the value was for an Excel addin so the value could be OPEN or OPEN1…or OPEN2 and so on..the problem here is that typical cmdlets like “where-object” are unable to parse the information to find the values you want to work with

In short after much searching I finally found a simple function that takes all of the items in a given registry location and places them into a custom object. From here the usual cmdlets we all know and love work well and we can easily manipulate the data we need to.

The original function was found in the comments section on Lee Holme’s site so thanks very much to “js2010” for that, it made my life a lot easier and hopefully if you’re reading this it works for you too 🙂

My version of the script is pasted below – usual disclaimers around using it in your environment apply..make sure you test etc.

In my case the way to determine if the registry string should be deleted was to find any values that matched my addin data (c:\temp\myaddin.xlam) but you should be able to adapt this script to suit any similar objective you may face:

# This script takes a registry location and puts it into a custom object which can then be parsed using where-object etc
# Version 1.0
# Set variables

$RegistryLocation = Get-ChildItem 'HKCU:\Software\Microsoft\Office\16.0\Excel\'
$MyData = '"c:\temp\myaddin.xlam"'

#Function configuration
function Get-RegistryItemProperty {
        param
        ([parameter(ValueFromPipeline)]$key)
        process 
        {
            $Key.getvaluenames() | 
            foreach-object {
            $value = $_
                [pscustomobject] @{
                Path = $Key -replace ‘HKEY_CURRENT_USER’,
                ‘HKCU:’ -replace ‘HKEY_LOCAL_MACHINE’,’HKLM:’
                Name = $Value
                Value = $Key.GetValue($Value)
                Type = $Key.GetValueKind($Value)
                }#end custom object creation
            } #end foreach loop
        } #end process block
     } #end function

# Call the function to search the registry location
$SearchRegistry = $RegistryLocation | Get-RegistryItemProperty

# If the value in $MyData is found, use where-object to manipulate the data further
  if($SearchRegistry.value -eq $MyData){
   $MyRegistryValue = $SearchRegistry | Where-Object {$_.value -eq $MyData }
   #Remove the Registry value using the returned data
   Remove-ItemProperty -Path $MyRegistryValue.Path -Name 
   $MyRegistryValue.Name -WhatIf 
}#end if

#Very basic debugging here
Else { Write-Host -fore Yellow "$MyData not found"}
        
            

If anyone feels it could be improved please leave a comment.

Thanks for reading

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.