PoshCode Logo PowerShell Code Repository

Difference between
modified post 6886 by Erhan Durucay on Fri 5th May 07:42 and
original post 2209 by Lee Holmes on Thu 9th Sep 21:42
Showold version | new version | both versions

    
11
##############################################################################
22
##
33
## Search-Registry
44
##
55
## From Windows PowerShell Cookbook (O'Reilly)
66
## by Lee Holmes (http://www.leeholmes.com/guide)
77
##
88
##############################################################################
1010
<#
1212
.SYNOPSIS
1414
Search the registry for keys or properties that match a specific value.
1616
.EXAMPLE
1818
PS >Set-Location HKCU:\Software\Microsoft\
1919
PS >Search-Registry Run
2121
#>
2323
param(
2424
    ## The text to search for
2525
    [Parameter(Mandatory = $true)]
2626
    [string] $Pattern
2727
)
2929
Set-StrictMode -Off
3131
## Helper function to create a new object that represents
3232
## a registry match from this script
3333
function New-RegistryMatch
3434
{
3535
    param( $matchType, $keyName, $propertyName, $line )
3737
    $registryMatch = New-Object PsObject -Property @{
3838
        MatchType = $matchType;
3939
        KeyName = $keyName;
4040
        PropertyName = $propertyName;
4141
        Line = $line
4242
    }
4444
    $registryMatch
4545
}
4747
## Go through each item in the registry
4848
foreach($item in Get-ChildItem -Recurse -ErrorAction SilentlyContinue)
4949
{
5050
    ## Check if the key name matches
5151
    if($item.Name -match $pattern)
5252
    {
5353
        New-RegistryMatch "Key" $item.Name $null $item.Name
5454
    }
5656
    ## Check if a key property matches
5757
    foreach($property in (Get-ItemProperty $item.PsPath).PsObject.Properties)
5858
    {
5959
        ## Skip the property if it was one PowerShell added
6060
        if(($property.Name -eq "PSPath") -or
6161
            ($property.Name -eq "PSChildName"))
6262
        {
6363
            continue
6464
        }
6666
        ## Search the text of the property
6767
        $propertyText = "$($property.Name)=$($property.Value)"
6868
        if($propertyText -match $pattern)
6969
        {
7070
            New-RegistryMatch "Property" $item.Name `
7171
                property.Name $propertyText
7272
        }
7373
    }
7474
}

ContributeMost Recent Contributions (feed)

Contribute ... Next Page