RSS

Using PowerShell list SharePoint site users, roles, groups on all sites

03 Feb

I have been asked to generate the list of users with their roles and groups belongs to for all sites presented in the web application. I found some blog talking about same requirements. But it is not fully satisfied my requirements. So I have taken his logic and created the PowerShell script to list the users, their roles and groups belong for the sites presented in the web application

Here is the below code which will iterate all sites and enumerate the users with their roles and groups belongs to.

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) > $null
function EnumerateUserRolesPermissions ([string]$webURL){
$site = new-object Microsoft.SharePoint.SPSite($webURL)
$web = $site.OpenWeb()
$webUsers = $web.Users
$groups = $web.sitegroups
foreach($webUser in $webUsers){
$Permissions = $web.Permissions
foreach($group in $groups)
{
foreach($Permission in $Permissions){
if($webUser.ID -eq $Permission.Member.ID){
foreach ($role in $webUser.Roles){
if ($role.Type -ne [Microsoft.SharePoint.SPRoleType]::None)
{
write-host $webURL,“;“,$webUser.LoginName,“;“,$webUser.Name,“;",$role.Type.ToString(),";",$webUser.groups
}
}
}
if($group.ID -eq $Permission.Member.ID){
foreach ($role in $group.Roles){
if ($role.Type -ne [Microsoft.SharePoint.SPRoleType]::None
{
foreach($user in $group.users){
write-host $webURL,“;“,$user.LoginName,“;“,$user.Name,“;",$role.Type.ToString(),";",$group.name
}
}
}
}
}
}
}
}
function EnumerateSiteUsers ()
{
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
foreach ($spService in $farm.Services) {
if (!($spService -is [Microsoft.SharePoint.Administration.SPWebService])) {
continue;
}
foreach ($webApp in $spService.WebApplications) {
if ($webApp -is [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]) { continue }
$webAppUrl = $webApp.GetResponseUri('Default').AbsoluteUri
foreach ($site in $webApp.Sites) {
foreach ($web in $site.AllWebs) {
EnumerateUserRolesPermissions $web.url
}
}
}
}
}

Hope this helps

Advertisements
 
14 Comments

Posted by on February 3, 2011 in PowerShell, Sharepoint

 

Tags: ,

14 responses to “Using PowerShell list SharePoint site users, roles, groups on all sites

  1. Balu

    February 7, 2011 at 8:13 pm

    Excellent. Thanks for this information and help us lot!

     
  2. Azi

    July 7, 2011 at 3:34 pm

    Hi Johnson,
    How can I export this to excel, or execute for a specific user?
    Thanks,

     
  3. AMOL GHUGE

    September 30, 2011 at 10:42 pm

    Thank you very much 4 sharing this vital information 🙂 🙂

     
  4. Dilip Nikam

    January 16, 2012 at 2:51 pm

    Thanks a lot…..

    Its save my time…..really nice Help….

     
  5. Dilip Nikam

    January 16, 2012 at 2:55 pm

    For the exporting you need to do the changes in the script as below….

    if ($role.Type -ne [Microsoft.SharePoint.SPRoleType]::None)
    {
    “URL~”+$webURL + “~LoginName~”+$webUser.LoginName+”~UserName~”+$webUser.Name+”~Role~”+$role.Type.ToString()+”~Group~”+$webUser.groups
    #$webURL,”~”,$webUser.LoginName,”~”,$webUser.Name,”~”,$role.Type.ToString(),”~”,$webUser.groups
    }

    foreach($user in $group.users)
    {
    “URL~”+$webURL + “~LoginName~”+$user.LoginName+”~UserName~”+$user.Name+”~Role~”+$role.Type.ToString()+”~Group~”+$group.name
    #$webURL,”;”,$user.LoginName,”;”,$user.Name,”;”,$role.Type.ToString(),”;”,$group.name
    }

    After adding the changes you need to call following command. It will create text file

    EnumerateUserRolesPermissions(“URL-Of-Your Site”) | format-table name > “C:\Result.txt”

    For export result in CSV you need to call the Export-Csv command.

    Thanks you….

     
  6. Kamaldeep

    May 3, 2012 at 6:42 am

    Hi,
    I am trying to use this query but facing issue…

    “Missing closing ‘)’ after expression in ‘if’ statement.
    At line:22 char:2
    + <<<< {
    + CategoryInfo : ParserError: (CloseParenToken:TokenId) [], Paren
    tContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisAfterStatement"

    Can you help !!!
    Kamaldeep

     
    • aubiera

      August 29, 2013 at 8:18 pm

      The if statement (on line 19 for me) is missing the closing “)”

      if ($role.Type -ne [Microsoft.SharePoint.SPRoleType]::None <<== Missing "("

       
  7. glazok28

    June 26, 2012 at 4:15 pm

    Hi all !

    Tried to run script with changes for export to file, but got an empty file. It’s necesseary to run script on Sharepoint Farm application server ?

     
  8. Detox From Methadone

    May 7, 2013 at 2:29 am

    Hey there! I could have sworn I’ve been to this website before but after browsing through some of the post I realized it’s new to me.
    Nonetheless, I’m definitely happy I found it and I’ll be bookmarking
    and checking back often!

     
  9. Geek

    August 1, 2013 at 2:03 pm

    Thanks a Lot

     
  10. Jonathan Miller

    July 25, 2014 at 8:19 pm

    We have been using an Axceler ControlPoint tool to generate a monthly user reports. But I dont think it satifies what we were looking for. However, this easy powershell script is more than enough to generate a monthly SharePoint user reports. Thanks so much for sharing and this would save a LOT of licensing cost for third party tools like Axceler ControlPoint (Crap product!)

     
  11. santosh

    June 18, 2015 at 7:01 pm

    can anyone write down the script where i can get it in CSV file. i copy, paste that and i get nothing. no result

     
  12. Paramvir

    August 20, 2015 at 5:12 pm

    When I tried to run the query, I got no results.
    Where do I give the URL for the Site/Site collection?

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: