Ever tried to search for a group by name but the part you know is in the middle? Did you think you would be smart and go to the advanced tab then do “blah” contains, hit search and find nothing?
Quickest way to find is actually via PowerShell
Get-ADGroup -Filter {Name -like “*blah*”} | select SAMAccountName
Works great!
Enjoy
-Eric
how do you search a list of group in such a way, such as, if you have 20 phrases to search for? I think it need input file and a loop to go through the input file but cant make it work!
I tried following but it doesn’t work 🙁 please help:
$InputFile=C:\users\ahmenaz-a\documents\GroupList.txt
$OutputFile=C:\users\ahmenaz-a\documents\grouplist_OUtPut.csv
$Groups = Get-Content -Path $InputFile
ForEach ($Group in $Groups)
{
$row = $Table.NewRow()
try {
$GetADGroup = get-ADGroup -identity {name -like $Group} -Properties *
$GetAdGroupName = $GetAdGroup.Name
$GetAdGroupDescription = $GetAdGroup.Description
$row.GroupName = “$GetAdGroupName”
$row.GroupDescription = “$GetAdGroupDescription”
[array]$GetAdGroupMemberOf = $GetAdGroup.MemberOf | Select-Object -First “20”
$GroupMemberOfCount = $GetAdGroupMemberOf.Length
For ($i=0; $i -lt $GroupMembersCount; $i++)
{
$GetAdGroupMembersName = Get-AdObject -Identity $GetAdGroupMembers[$i] | Select-Object -Property Name
$DynamicMembersColumnHeader = “Members$i”
$row.$DynamicMembersColumnHeader = $GetAdGroupMembersName.Name
}
}
# Catch the error if the group in the .txt file does not exist in AD.
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{
$GetAdGroupName = “$Group”
$GetAdGroupDescription = “Group Not Found”
$row.GroupName = “$GetAdGroupName”
$row.GroupDescription = “$GetAdGroupDescription”
}
# Commit the new row to the table.
$Table.Rows.Add($row)
# Display the table in the PowerShell console.
Write-Host “Here is a partial view of the table. The CSV file has all of the columns.”
$Table | Format-Table -AutoSize
# Export the table to a CSV file.
$Table | Export-Csv -Path $OutputFile -NoTypeInformation
}
Its well beyond what I was trying to post here but here are the basics.
You can search for multiple words at once by using -or like so:
Get-ADGroup -Filter {Name -like “*blah*” -or -like “*whatever*”}
Now if you need to supply that list of words dynamically then you need to use a scriptBlock where you look through your word list essentially building a text string of the entire where clause, then convert it to a scriptBlock and leverage that. Some info on that can be found here:
https://ramblingcookiemonster.wordpress.com/2014/11/30/quick-hit-dynamic-where-object-calls/