Powershell offers a number of Active Directory (AD for short) commandlets to make an AD admin’s life a little easier. For example, if you need to get a list of members from an AD group, you can use something like:
Get-ADGroupMember -Identity 'Enterprise Admins' -Recursive
The problem is that this doesn’t work everywhere. The ActiveDirectory module is not a “normal” one you can install with Install-Module; instead, you need to install a Windows feature, either from Control Panel or by using the Add-WindowsCapability commandlet.
But you don’t have to use this module. You can use something that’s available everywhere, the adsiSearcher type accelerator.
So here are a couple of scripts I came up with (credits where they’re due). The first searches through all groups, finds all the ones that match a string and lists all their members.
## Source: DotJim blog (https://dandraka.com)# Jim Andrakakis, January 2024# Updated April 2026 to add regex# Thanks to Diego for the inspiration!# # ===== Parameters ===== param( # use either a substring or a regex # e.g. AccountingGroup\d\d would match # AccountingGroup01, AccountingGroup02 etc [string]$searchString = 'accounting', # if true, lists all group members [bool]$listMembers = $true, # if true, regards the searchString as regex # if false, does a simple substring match [bool]$useRegEx = $false) # ======================Clear-Host$ErrorActionPreference = 'Stop'# === Get all groups ===$objSearcher = [adsisearcher]'(&(objectCategory=group))'$objSearcher.PageSize = 20000 # may need to adjust, though should be enough for most cases# specify properties to include$colProplist = "name"foreach ($i in $colPropList) { $objSearcher.PropertiesToLoad.Add($i) | out-null } $colResults = $objSearcher.FindAll()foreach ($objResult in $colResults) { #group name $group = $objResult $groupname = ($objResult.Properties).name if ($useRegEx) { if (-not ($groupname[0] -match $searchString)) { continue } } else { if (-not ($groupname[0].ToLower().Contains($searchString.ToLower()))) { continue } } Write-Host "* $groupname [$($group.Path)]" if ($listMembers) { $Group = [ADSI]$group.Path $groupMembers = $Group.Member if ($groupMembers.Count -eq 0) { Write-Host "`t(No members)" continue } $groupMembers | ForEach-Object { $Searcher = [adsisearcher]"(distinguishedname=$_)" $member = $searcher.FindOne() $userName = $member.Properties.samaccountname $name = $member.Properties.displayname Write-Host "`t[$userName]`t$name" } }}
The second displays all details of all users whose name matches a substring.
## Source: DotJim blog (https://dandraka.com)# Jim Andrakakis, January 2024# # ===== Parameters ===== param( [string]$searchString = 'Papadomanolakis') # ====================== Clear-Host$ErrorActionPreference='Stop' # === Get all groups ===$objSearcher=[adsisearcher]"(&(objectClass=user)(displayname=*$($searchString)*))"$objSearcher.PageSize = 20000 # may need to adjust, though should be enough for most cases#$objSearcher.FindOne().Properties.Keys$objSearcher.FindAll() | % { $_.Properties }
The third one is a brilliant one-liner by Jos Lieben that lists all groups of a user.
$userName = $env:USERNAME # change if different user needed([ADSISEARCHER]"(member:1.2.840.113556.1.4.1941:=$(([ADSISEARCHER]"samaccountname=$userName").FindOne().Properties.distinguishedname))").FindAll().Properties.distinguishedname -replace '^CN=([^,]+).+$','$1'
And now an addition two years later 😊 a quick one-liner to get the emails of everyone that belongs to certain groups, in case you need to send a mass email. Each email will appear only once, even if a user is member of more than one of the groups specified (“distinct”).
$groups = "myADgroup1,myADgroup2,myADgroup3"; (($groups -split ',' | ForEach-Object { $g = ([adsisearcher]"(&(objectCategory=group)(cn=$($_.Trim())))").FindOne(); if ($g) { ([adsisearcher]"(&(objectCategory=person)(objectClass=user)(mail=*)(memberOf:1.2.840.113556.1.4.1941:=$($g.Properties.distinguishedname[0])))").FindAll() | ForEach-Object { $_.Properties.mail[0] } } } | Where-Object { $_ } | Sort-Object -Unique) -join ';')
Hope that helps. Enjoy! 😊