Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of Active Directory group names beginning with "ABC_" in PowerShell?

I am new to PowerShell and I am trying to get a list of Active Directory items that start with the same naming convention for example I have a number of groups beginning with "ABC_Group1", "ABC_Group2", "ABC_Group3".

I know that:

get-adgroup "ABC_Group1"

will list that specific group

'get-adgroup -filter * | sort name | select Name' 

will list all the groups but I don't know how to filter to find just the specific groups starts with "ABC_"

I then want to list it's members.

like image 874
A.Codes Avatar asked Jan 24 '26 13:01

A.Codes


2 Answers

You can use Wildcard search with Where condition. In the newer PS version, the where clause can be used as Filter

Import-Module ActiveDirectory

Get-ADGroup -Filter {Name -like 'ABC_*'}  -Properties * | select -property SamAccountName,Name,Description,DistinguishedName,CanonicalName,GroupCategory,GroupScope,whenCreated

Since the OP asked to get the members of the group as well, here is the piece of code which will help you:

Get-ADGroup -Filter {Name -like 'ABC_*'} -SearchBase "DC=YourDC" | Get-ADGroupMember -Partition "DC=YourDC"

OR

Get-ADGroup 'Group Name' -Properties Member | Select-Object -ExpandProperty Member

OR use Dot notation:

(Get-ADGroup 'Group Name' -Properties Member).Member

Hope this helps.

like image 86
Ranadip Dutta Avatar answered Jan 26 '26 22:01

Ranadip Dutta


I've done some research and played around with the code and it turns out,

Get-ADGroup -Filter "name -like '*ABC_*'" | sort name lists all the groups that have "ABC_"

However, this also means it will list directories such as "Group_ABC_". However, I only want to list directories that START with "ABC_"

like image 38
A.Codes Avatar answered Jan 26 '26 22:01

A.Codes