How to get all groups that a user is a member of?

后端 未结 30 1748
攒了一身酷
攒了一身酷 2020-12-04 05:56

PowerShell\'s Get-ADGroupMember cmdlet returns members of a specific group. Is there a cmdlet or property to get all the groups that a particular user is a member of?

30条回答
  •  难免孤独
    2020-12-04 06:35

    I couldn't get the following to work for a particular user:

    Get-ADPrincipalGroupMembership username
    

    It threw an error that I was not willing to troubleshoot.

    I did however come up with a different solution using Get-ADUser. I like it a bit better because if you don't know the account name then you can get it based off of a wildcard on the user's actual name. Just fill in PartOfUsersName and away it goes.

    #Get the groups that list of users are the member of using a wildcard search
    
    [string]$UserNameLike = "*PartOfUsersName*" #Use * for wildcards here
    [array]$AccountNames = $(Get-ADUser -Filter {Name -like $UserNameLike}).SamAccountName
    
    ForEach ($AccountName In $AccountNames) {
    Write-Host "`nGETTING GROUPS FOR" $AccountName.ToUpper() ":"
    (Get-ADUser -Identity $AccountName -Properties MemberOf|select MemberOf).MemberOf|
        Get-ADGroup|select Name|sort name
        }
    

    Huge props to schmeckendeugler and 8DH for getting me to this solution. +1 to both of you.

提交回复
热议问题