MyADTools.psm1

function Search-ADGroup {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $string
    )
begin{
$DomainName = $env:USERDNSDOMAIN 

write-host "Finding all the AD groups in $DomainName" 
Import-Module activedirectory
$alladgroups = Get-ADGroup -Filter * -Properties *
$countnamematches=@()
$countdescmatches=@()
# Look through each AD Group name and description
Write-Host "Starting search...." 
}
process{
foreach ($adgroup in $alladgroups) { 
    if ($adgroup -match $string) { 
        write-host "AD name match found in: $($adgroup.Name)"
        $countnamematches+=$adgroup
        }
    }
    
foreach ($adgroup in $alladgroups) { 
    if (($adgroup.Description -match $string) -and (($adgroup -notmatch $string))) { 
        write-host "Description match found in: $($adgroup.Name) Description: '$($adgroup.description)'" 
        $countdescmatches+=$adgroup
        }
    }
    
Write-Host "*Search ended*"
}
end{
    if ($null -eq $countnamematches) {
        Write-Host "Searchstring was not found in any AD group name"
    }
    elseif ($null -eq $countdescmatches) {
        Write-Host "Searchstring was not found in any AD group description"
    }
    elseif (($null -eq $countnamematches) -and ($null -eq $countdescmatches)) {
        Write-Host "Searchstring was not found anywhere"
    }
    else {
        Write-Host "Search results: `nMatches in name: $($countnamematches.count) `nMatches in description: $($countdescmatches.count) `nTotal matches: $(($countnamematches.count)+($countdescmatches.count))" -ForegroundColor Green -BackgroundColor Black
    }
}
}


function Search-GPO {
    [CmdletBinding()]
    param (
        [parameter(Mandatory,
                ValueFromPipeline=$true,
                ParameterSetName="String",
                HelpMessage="What string do you want to search for?")]
        $String
              
    )
    
    begin {
        # Set the domain to search for GPOs
        $DomainName = $env:USERDNSDOMAIN 
 
        # Find all GPOs in the current domain
        write-host "Finding all the GPOs in $DomainName" 
        Import-Module grouppolicy 
        $allGposInDomain = Get-GPO -All -Domain $DomainName 
        $FoundGPOs = New-Object -TypeName System.Collections.ArrayList
        $ProgressCount=1
        Write-Host "Starting search...." 
    }
    
    process {
        foreach ($gpo in $allGposInDomain) { 
            $report = Get-GPOReport -Guid $gpo.Id -ReportType Xml 
            [int]$progress=$(($ProgressCount/$allGposInDomain.count)*100.)
            Write-Progress -Activity "Searching GPOs for the string: $($String)" -Status "$($progress)% Complete" -PercentComplete (($ProgressCount/$allGposInDomain.count)*100)
            $ProgressCount++
            if ($report -match $String) { 
                write-host "********** Match found in: $($gpo.DisplayName) **********" 
                $FoundGPOs+=$gpo
            }
    }
}
    
    end {
        Write-Progress -Activity "Searching GPOs for the string: $($String)" -Status "$($progress)% Complete" -PercentComplete 100
        if ($FoundGPOs){
        Write-Host "Search completed with $($FoundGPOs.count) matches shown above" 
        }
        else{
        Write-Host "Search completed with $($FoundGPOs.count) matches"
        }
    }
}