kwizcom banner advertisement

How to Check If SharePoint User is Member of a Active Directory Group using PowerShell?

Requirement: Check If SharePoint User is Member of a Active Directory Group using PowerShell?

Solution: Although we can check if a particular user account is member of a AD security group using AD PowerShell cmdlets such as Get-ADGroupMember Here is the native method to find if a particular user account is a member of a given Active directory group.

PowerShell to Check if SharePoint User is Member of a Active Directory Group:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteURL="https://portal.crescent.com/sites/test/"
$SearchUserAccount = "Crescent\salaudeen"
 
#Custom Function to Check if User is member of a Active Directory Group
Function Check-UserIsMemberOfADGroup($web,$SearchUserAccount,$GroupName)
{
    $ReachedMax = $false
    
    #Get All Users of the AD Group
    $Users = [Microsoft.SharePoint.Utilities.SPUtility]::GetPrincipalsInGroup($web, $GroupName, 10000, [ref]$ReachedMax) 

    #Check if user found in the members list
    $SearchUser = $Users | Where {$_.LoginName -eq $SearchUserAccount}
    If($SearchUser -ne $Null) { Return $True } else { Return $False }
}

#Get the Web
$Web = Get-SPWeb $SiteURL

#Get All AD Groups which has permission to the object such as Web, List, etc
$RoleAssignments = $Web.RoleAssignments | Where { $_.Member.IsDomainGroup}
    
#Iterate Through permissions of the web
Foreach($RoleAssignment in $RoleAssignments)
{
    $IsMember = Check-UserIsMemberOfADGroup $Web $SearchUserAccount $RoleAssignment.Member.Name
    If($IsMember)
    {
        Write-host -f Green $SearchUserAccount is member of the AD Group $RoleAssignment.Member.Name
    }
    Else
    {
        Write-host -f Red $SearchUserAccount is not a member of the AD Group $RoleAssignment.Member.Name
    }
} 
How to Check If SharePoint User is Member of a Active Directory Group using PowerShell? How to Check If SharePoint User is Member of a Active Directory Group using PowerShell? Reviewed by Salaudeen Rajack on September 05, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.