Showing posts with label SharePoint 2010. Show all posts
Showing posts with label SharePoint 2010. Show all posts

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!

Monday, February 16, 2015

Web Application : Retrieve All Users Assigned to Groups in Site Collections (SP2010 & SP2013)

Issue:

You are requested to retrieve all users in all groups inside all site collections under a web application.

Solution:

Run this PowerShell script in SharePoint Management Shell

#Header
"SiteCollectionURL;GroupName;PermissionLevel;UserName;UserID"

Try
{
 $WebApplicationUrl = "http://contoso.com" 
 $webApplication = Get-SPWebApplication $WebApplicationUrl -ErrorAction SilentlyContinue;
  
 if($webApplication -ne $null)
 {
  foreach($site in $webApplication.Sites){
   #Get all Site Collection Administrator
   foreach ($siteAdmin in $site.RootWeb.SiteAdministrators)
   {
    $site.URL+";Site Administrator;Full Control;"+$siteAdmin.DisplayName+";"+$siteAdmin.UserLogin
   }
   
   
   $groups = $site.RootWeb.SiteGroups
   foreach ($group in $groups) 
   {
    foreach ($user in $group.users) 
    {
     $site.URL+";"+$group.Name+";"+$group.Roles+";"+$user.DisplayName+";"+$user.UserLogin
    } 
   }
   $site.Dispose()
  }
 }
 else
 {
  Write-Host "Could not find Web Application $WebApplicationUrl" -ForegroundColor Red;
 }
}
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!

For SP2007 solution, click here.