Home » PowerShell » Get AdGroupMember SAMAccountName in PowerShell

Get AdGroupMember SAMAccountName in PowerShell

To get adgroupmember samaccountname in PowerShell, use the Get-AdGroupMember cmdlet and specify the group name to get a list of members with their name and samaccountname.

The Get-AdGroupMember cmdlet in PowerShell gets the member of an active directory group and ad objects properties.

In this article, we will discuss how to get adgroup member samaccountname in PowerShell using the Get-AdGroupMember cmdlet.

Get AdGroup Member SAMAccountName

To get adgroupmember samaccountname for users in the specified active directory group, use the Get-AdGroupMember.

Get-ADGroupMember -Identity "SALESLEADER" | Select Name, SamAccountName, objectClass

In the above PowerShell script, the Get-AdGroupMember uses the Identity parameter to specify the adgroup name and gets all the members including users, and groups.

It pipes the output to the Select command to get adgroup member samaccountname, name, and ObjectClass.

The output of the above PowerShell script to get samaccountname for adgroup members is:

Get AdGroup Member SAMAccountName
Get AdGroup Member SAMAccountName

Get AdGroupMember SAMAccountName for User in PowerShell

To get aduser samaccountname for adgroup members, use the Get-AdGroupMember cmdlet with the Where condition to filter ObjectClass is equal to the user only.

Get-ADGroupMember -Identity "SALESLEADER" | where {$_.objectclass-eq "user"} | Select Name, SamAccountName

In the above PowerShell script, the Get-AdGroupMember cmdlet uses the Identity parameter to specify the adgroup name and gets the members of the adgroup.

It pipes the members of the adgroup to the Where condition to use filter where ObjectClass is equal to the user only and get adgroupmember samaccountname for users.

The output of the above script to find samaccountname for users in adgroup is:

PS C:\> Get-ADGroupMember -Identity "SALESLEADER" | where {$_.objectclass-eq "user"} | Select Name, SamAccountName

Name                                SamAccountName
----                                       --------------
Tom Smith                        toms
Chris Dore                        chrisd
Gary Waugh                     gary.waugh


PS C:\>

Get AdGroupMember SAMAccountName for AdGroup in PowerShell

To get adgroupmember samaccountname for adgroup, use the Get-AdGroupMember with where condition to check ObjectClass is equal to the group.

Get-ADGroupMember "SALESLEADER" | where {$_.objectclass-eq "group"} | Select Name, SamAccountName

In the above PowerShell script, the Get-AdGroupMember gets the group members and uses the Where condition to filter group members for adgroup only and get samaccountname for adgroup.

The output of the above PowerShell script to find adgroupmember samaccountname for adgroup is:

PS C:\> Get-ADGroupMember "SALESLEADER" | where {$_.objectclass-eq "group"} | Select Name, SamAccountName

Name                           SamAccountName
----                                 --------------
DL-SALES                     DL-SALES


PS C:\>

Conclusion

I hope the above article on how to get adgroupmember samaccountname for user and group is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.