Share via


Get all Groups in a Site Collection Using PowerShell

**Get all Groups in a Site Collection Using PowerShell **

PowerShell has been the preferred option by SharePoint professionals when it comes to administrative tasks and doing single time activities.
 
Few weeks back i had a requirement to check where few SPGroups were used. I had the name of those SharePoint Groups and had to zero in on where those groups were used. I had about 100 sites in my site collection. So, I decided to write a PowerShell Script to cater to my requirement.
 

01.if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
02.{
03.            Add-PSSnapin "Microsoft.SharePoint.PowerShell"
04.}
05.$SPFile = ".\GetGroupInformation.txt"
06.$SPSiteUrl = Read-Host "Enter the Url of the Site Collection "
07.$SPSite = Get-SPSite $SPSiteUrl -ErrorAction SilentlyContinue
08. 
09.if($SPSite -ne $null){
10.$SPWebCollection = $SPSite.AllWebs
11.foreach($SPWeb in  $SPWebCollection){
12.Write-Output $SPWeb.Title "contains the following groups"| Out-File $SPFile -append
13.Write-Output "========================"| Out-File $SPFile -append
14.foreach($SPGroup in  $SPWeb.Groups){
15.Write-Output $SPGroup.Name| Out-File $SPFile -append
16.}
17.Write-Output "========================"| Out-File $SPFile -append
18.}
19.}
20.else
21.{
22.Write-Host "Requested Site Could Not be found"  -ForegroundColor DarkRed
23.}

Note that i have used "Write-Output" to write the group information to a text file so that it would be easy to go through the log information saved in the text file.
 
Hope this would be of  help to few of you.

Happie Coding :)