none
PowerShell - Active Directory - Get memberOf RRS feed

  • Question

  • Hi Guys

    How do I get the member of but bringing the sAMAccountName instead of the distinguishedName of the group?

    Friday, March 10, 2023 6:52 PM

All replies

  • To get the members of a group in Active Directory using PowerShell and bring the sAMAccountName of the group instead of the distinguishedName, you can use the Get-ADGroupMember cmdlet with the -Identity parameter and then select the sAMAccountName property of each member object returned.

    Here's an example code snippet:

    $groupName = "Group Name"
    $group = Get-ADGroup -Identity $groupName
    $members = Get-ADGroupMember -Identity $group | Select-Object -Property sAMAccountName
    $members.sAMAccountName

    In this code, we first define the group name as a variable, and then we use the Get-ADGroup cmdlet to retrieve the group object by its name. We then pass this group object to the Get-ADGroupMember cmdlet to retrieve the group members. Finally, we use the Select-Object cmdlet to select only the sAMAccountName property of each member object and output it to the console.

    This code should return a list of sAMAccountName values for the members of the group specified by $groupName.
    Friday, March 10, 2023 10:00 PM
  • Saludos Sergio, hace poco estuve con un caso muy similar, y la forma en como pude solucionarlo fue de la siguiente manera:

    get-aduser -filter * -properties samaccountname,enabled,MemberOf |
    select samaccountname,enabled,@{Name='MemberOf';Expression={$_.Memberof -join ';'}}

    Espero te sirva el dato.

    Jorge Vega



    Jorge

    Thursday, March 16, 2023 4:09 AM