Wednesday, August 26, 2015

Powershell : Retrieve All Users in Site Collections under Web Application

Issue:

You are requested to retrieve all users in all site collections under a web application in SP2010/2013.

Solution:

Run this PowerShell script in Microsoft SharePoint Management Shell

#Header
"SiteCollectionURL;LoginName"
 
Try
{
 $WebApplicationUrl = "http://contoso.com"
 $webApplication = Get-SPWebApplication $WebApplicationUrl -ErrorAction SilentlyContinue;

 foreach($site in $webApplication.Sites){
  if($site.ReadOnly -eq $null -and $site.ReadLocked -eq $null -and $site.WriteLocked -eq $null)
  {
   continue;
  }
  else
  {
   foreach($user in $site.RootWeb.AllUsers){
    $site.RootWeb.URL +";"+ $user.LoginName;
   }
  }
 }
}
Catch [system.exception]
{
 "Error Occurred: $_"
}

Usually I will just simply save the code as in PS1 file and use this command to save it into CSV file.

file.ps1 > result.csv

And then use Excel to massage the data accordingly. Have fun!

No comments:

Post a Comment