Scripts/Search-ADGroup.ps1

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
    }
}
}