Wednesday, May 16, 2012

Find if a user belongs to any SharePoint groups in a site collection

Check permissions function in the site permssion (_layouts/user.aspx) just shows the groups where the user is part of for that particular web (SPWEB). What about the scenario when the user could be part of a SharePoint group on a few sub webs within the site collection? Going to each sub web and checking permissions??



PowerShell to the rescue...

function UserInGroups ($UserName)

{
   $site = Get-SPSite "http://your_site_collection_url"
   $groups = $site.RootWeb.sitegroups
   foreach ($grp in $groups)
   {
      foreach ($user in $grp.users)
      {
         if($user.Name.ToLower().Contains($UserName.ToLower()))
           {
               Write-Host $user.Name "("$user.loginname") Group: " $grp.name
           }
      }
   }
  $site.Dispose()
}

Here is the snapshot comparing the results of OOTB Check Permissions Vs. the PowerShell one that does it for the entire site collection. You will notice that the OOTB one shows results for the current web only when the powershell one gets all the SharePoint groups for the given user.


Happy SharePointing... :)

No comments: