PowerShell Script to Find All Active Directory Groups in SharePoint

Requirement: Get the list of All AD Security groups used in SharePoint sites. We need to generate a report on AD groups that are being used in a SharePoint web application.
PowerShell script to find AD Groups in SharePoint: Here is my PowerShell script to find and export Active Directory groups on all SharePoint sites with in the given web application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Change to your web application
 
#Get Web Application
$WebApp = Get-SPWebApplication $WebAppURL
 
#variable for data collection
$ADGroupCollection= @()
$ReportPath ="C:\ADGroups.csv"
 
foreach ($Site in $WebApp.Sites)
{
    Write-host -foregroundcolor green "Processing Site Collection: "$site.RootWeb.URL
     
    #Get all AD Security Groups from the site collection
    $ADGroups = Get-SPUser -Web $Site.Url | Where { $_.IsDomainGroup -and $_.displayName -ne "Everyone" }
 
    #Iterate through each AD Group
    foreach($Group in $ADGroups)
    {
            Write-host "Found AD Group:" $Group.DisplayName
 
            #Get Direct Permissions
            $Permissions = $Group.Roles | Where { $_.Name -ne "Limited Access" } | Select -ExpandProperty Name
 
            #Get SharePoint User Groups where the AD group is member of.
            $SiteGroups = $Group.Groups | Select -ExpandProperty Name
 
            #Send Data to an object array
            $ADGroup = new-object psobject
            $ADGroup | add-member noteproperty -name "Site Collection" -value $Site.RootWeb.Title
            $ADGroup | add-member noteproperty -name "URL" -value $Site.Url
            $ADGroup | add-member noteproperty -name "Group Name" -value $Group.DisplayName
            $ADGroup | add-member noteproperty -name "Direct Permissions" -value ($Permissions -join ",")
            $ADGroup | add-member noteproperty -name "SharePoint Groups" -value ($SiteGroups -join ",")
            #Add to Array
            $ADGroupCollection+=$ADGroup          
    }
}
    #Export Data to CSV
    $ADGroupCollection | export-csv $ReportPath -notypeinformation
    Write-host "SharePoint Security Groups data exported to a CSV file at:"$ReportPath -ForegroundColor Cyan

This script generates a CSV file report with output:

  • Site collection Name and URL
  • Active Directory group name
  • Permissions applied to the AD group either by direct permission level or via SharePoint groups.


Read more: http://www.sharepointdiary.com/2014/11/powershell-to-find-active-directory-groups-in-sharepoint.html#ixzz4olnX93zT

posted @ 2017-08-04 15:39  【上海】Peter  阅读(390)  评论(0)    收藏  举报