PoshCode Logo PowerShell Code Repository

Search-Registry.ps1 by Erhan Durucay 3 weeks ago (modification of post by Lee Holmes view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6886"></script>download | new post

From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes

  1. ##############################################################################
  2. ##
  3. ## Search-Registry
  4. ##
  5. ## From Windows PowerShell Cookbook (O'Reilly)
  6. ## by Lee Holmes (http://www.leeholmes.com/guide)
  7. ##
  8. ##############################################################################
  9.  
  10. <#
  11.  
  12. .SYNOPSIS
  13.  
  14. Search the registry for keys or properties that match a specific value.
  15.  
  16. .EXAMPLE
  17.  
  18. PS >Set-Location HKCU:\Software\Microsoft\
  19. PS >Search-Registry Run
  20.  
  21. #>
  22.  
  23. param(
  24.     ## The text to search for
  25.     [Parameter(Mandatory = $true)]
  26.     [string] $Pattern
  27. )
  28.  
  29. Set-StrictMode -Off
  30.  
  31. ## Helper function to create a new object that represents
  32. ## a registry match from this script
  33. function New-RegistryMatch
  34. {
  35.     param( $matchType, $keyName, $propertyName, $line )
  36.  
  37.     $registryMatch = New-Object PsObject -Property @{
  38.         MatchType = $matchType;
  39.         KeyName = $keyName;
  40.         PropertyName = $propertyName;
  41.         Line = $line
  42.     }
  43.  
  44.     $registryMatch
  45. }
  46.  
  47. ## Go through each item in the registry
  48. foreach($item in Get-ChildItem -Recurse -ErrorAction SilentlyContinue)
  49. {
  50.     ## Check if the key name matches
  51.     if($item.Name -match $pattern)
  52.     {
  53.         New-RegistryMatch "Key" $item.Name $null $item.Name
  54.     }
  55.  
  56.     ## Check if a key property matches
  57.     foreach($property in (Get-ItemProperty $item.PsPath).PsObject.Properties)
  58.     {
  59.         ## Skip the property if it was one PowerShell added
  60.         if(($property.Name -eq "PSPath") -or
  61.             ($property.Name -eq "PSChildName"))
  62.         {
  63.             continue
  64.         }
  65.  
  66.         ## Search the text of the property
  67.         $propertyText = "$($property.Name)=$($property.Value)"
  68.         if($propertyText -match $pattern)
  69.         {
  70.             New-RegistryMatch "Property" $item.Name `
  71.                 property.Name $propertyText
  72.         }
  73.     }
  74. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me