Discovering Who Has Access to SharePoint 2010 Securable Objects

Posted on Posted in Scripts, SharePoint 2010

I’ve talked on several occasions about how we can easily use the SharePoint 2010 object model (OM) to discover who has access to a securable object (SPWeb, SPList, or SPListItem) and the fact that we can use the same mechanisms within PowerShell to create useful security/audit reports. On some of those occasions I’ve shown a version of a PowerShell script which gives you a dump to the screen or a text file of every securable object and who has access to it and how they were given access to it – today I’d like to share a new version of that script.

Before we get to the actual script let’s first talk about how to get the information. All securable objects have a method named GetUserEffectivePermissionInfo which is defined in the abstract base class SPSecurableObject (in 2007 this method was defined directly on the SPWeb, SPList, and SPListItem objects). This method returns back an SPPermissionInfo object which we can use to inspect the various role definition bindings and corresponding permission levels.

Once we have the permission details we simple loop through the SPRoleAssignments objects via the RoleAssignments property. This will give us information about how the user is given access to the resource. Next we look at the RoleDefinitionBindings property which returns back a collection of SPRoleDefinition objects that tell us about the type of access granted (e.g., Full Control, etc.).

I then take all this information, stick it in a hash table which I then use to create a new object which gets written to the pipeline.

So with that, let’s take a look at the code:

Great – we’ve got the code – so now you’re probably asking, "how the heck do I use it?" Well the first thing you need to do is save it to a file, let’s call it SecurityReport.ps1 and we’ll put it in the root of the C drive. Once saved we can load it in memory using the following:

C:\ PS> . .\SecurityReport.ps1

Now for the fun stuff :). The examples I’m going to show will build off of each other and will eventually conclude with an example that gives me a report for all users and all securable objects throughout the entire farm. The first example I want to show is how to retrieve a report for a single user and a single web (we’ll reuse the $user variable throughout the script so I’ll only define it once here):

$user = "sp2010\siteowner2"
Get-SPWeb http://portal | Get-SPUserEffectivePermissions $user | Out-GridView -Title "Web Permissions for $user"

Running this command will generate a grid view as shown here:


Note that I could have just as easily saved the results to a CSV file which I could then open in Excel using the Export-Csv cmdlet:

Get-SPWeb http://portal | Get-SPUserEffectivePermissions $user | Export-Csv -NoTypeInformation -Path c:\perms.csv

For this next example I’m going to show the permissions for the same user for ALL webs throughout the entire farm (note that this won’t include lists or items):

Get-SPSite -Limit All | Get-SPWeb | Get-SPUserEffectivePermissions $user | Out-GridView -Title "All Web Permissions for $user"

Now I want to get the permissions for the same user for all lists throughout the entire farm:

Get-SPSite -Limit All | Get-SPWeb | %{$_.Lists | Get-SPUserEffectivePermissions $user} | Out-GridView -Title "List Permissions for $user"

Now we’re going to get nice and deep and show the permissions for every single item throughout the entire farm (probably don’t want to run this on any front-end servers):

Get-SPSite -Limit All | Get-SPWeb | %{$_.Lists | %{$_.Items | Get-SPUserEffectivePermissions $user}} | Out-GridView -Title "Item Permissions for $user"

So now that I’ve shown you how to get the individual securable objects results throughout the farm for a single user let’s now go ahead and stitch them together into one report:

Get-SPSite -Limit All | ForEach-Object {
    $site = $_
    $webPermissions += $site | Get-SPWeb –Limit All | Get-SPUserEffectivePermissions $user
    $listPermissions += $site | Get-SPWeb –Limit All | %{$_.Lists | Get-SPUserEffectivePermissions $user}
    $itemPermissions += $site | Get-SPWeb –Limit All | %{$_.Lists | %{$_.Items | Get-SPUserEffectivePermissions $user}}
$webPermissions + $listPermissions + $itemPermissions | Out-GridView -Title "Web, List, and Item Permissions for $user"

In this example I’m simply performing the same calls but appending to an array of objects and then dumping the combination of those arrays to the grid. Note that in this case I’m calling $site.Dispose() but below I’ll be using the SPAssignmentCollection to dispose of objects – keep reading for an explanation.

So now lets take it one step further and see how we can get the same reports but this time for every user. We’ll start with webs again – in this example we’ll get the permissions for all users for a given site:

$gc = Start-SPAssignment
$site = $gc | Get-SPSite http://portal
$site | Get-SPWeb –Limit All | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName) | Out-GridView -Title "Web Permissions for All Users In $($site.Url)"
$gc | Stop-SPAssignment

As you can see I’m basically using the SiteUsers property from the root web and passing the login name for each user into the function. Note that here I’m using the Start-SPAssignment and Stop-SPAssignment cmdlets – that’s because I’m using the SPSite object after the pipeline execution finishes (as opposed to the above) so I need to make sure it gets disposed (I could just as easily called Dispose on the object as I did above but I’m attempting to demonstrate when/why you’d use the assignment collections).

Now lets see the lists:

$gc = Start-SPAssignment
$site = $gc | Get-SPSite http://portal
$site | Get-SPWeb –Limit All | %{$_.Lists | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)} | Out-GridView -Title "List Permissions for All Users in $($site.Url)"
$gc | Stop-SPAssignment

Starting to see a pattern? Let’s take a look at the list items now:

$gc = Start-SPAssignment
$site = $gc | Get-SPSite http://portal
$site | Get-SPWeb –Limit All | %{$_.Lists | %{$_.Items | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)}} | Out-GridView -Title "Item Permissions for All Users in $($site.Url)"
$gc | Stop-SPAssignment

Great! So now lets piece this last bit together so we can see the permissions for all webs, lists, and list items for every user within a single site collection:

$gc = Start-SPAssignment
$site = $gc | Get-SPSite http://portal
$webPermissions = $site | Get-SPWeb –Limit All | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)
$listPermissions = $site | Get-SPWeb –Limit All | %{$_.Lists | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)}
$itemPermissions = $site | Get-SPWeb –Limit All | %{$_.Lists | %{$_.Items | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)}}
$webPermissions + $listPermissions + $itemPermissions Out-GridView -Title "Web, List, and Item Permissions for All Users in $($site.Url)"
$gc | Stop-SPAssignment

Alright, we’re almost done – let’s now stitch this all together and generate a single report showing all permissions for all securable objects (webs, lists, and list items) for every user within every site collection:

Get-SPSite -Limit All | ForEach-Object {
    $site = $_
    $webPermissions += $site | Get-SPWeb –Limit All | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)
    $listPermissions += $site | Get-SPWeb –Limit All | %{$_.Lists | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)}
    $itemPermissions += $site | Get-SPWeb –Limit All | %{$_.Lists | %{$_.Items | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)}}
$webPermissions + $listPermissions + $itemPermissions | Out-GridView -Title "Web, List, and Item Permissions for All Users in All Sites"

Note in this last example, as I did previously when looping through all site collections, I’m calling the Dispose() method inside the ForEach-Object script block. I do this because objects wouldn’t otherwise get disposed until the pipeline execution has finished and because it’s continuing to iterate so the pipeline has not yet completed. If I used the assignment collection I wouldn’t get a disposal until after I’m done iterating which would be too late – I want to dispose right when I’m done with the individual SPSite objects to avoid out of memory errors.

Reporting on who has access to what is one of the things I get asked about most frequently so hopefully this code sample and corresponding examples will prove to be useful to people. One possible area of improvement to the script would be to accommodate groups being passed in – right now I’m only considering users; and of course you could easily turn the example usages into functions. As always, if anyone has any feedback (bugs, improvements, etc.) please post here so that myself and others may benefit.

38 thoughts on “Discovering Who Has Access to SharePoint 2010 Securable Objects

  1. Gary,
    This looks very good for 2010 which we are implementing at my company starting Jan 2011. However, could this powershell script be altered to do the same thing for MOSS 2007? Moreover, if you can get the security objects, could one also make this replace/copy the security (permissions)? For instance, johndoe needs all of janedoe’s permissions. If we know janedoe’s security with your cmdlet, we should be able to add or replace that security for johndoe, right?

    Why I ask…we have just combined several AD domains into one new AD domain and all of the users are being replicated to the new domain and they will still need their same SharePoint permissions. Because funds are tight (no 3rd party tool), I am trying to define a way to code/script the process of replacing or copying security for DOMAIN1\john to DOMAIN2\john on the same farm. I guess, the first thing I should ask is does that sound doable?
    Thanks in advance for you thoughts.

  2. Gary thanks for the tip. I went with the MigrateUser command. I thought based on the name that it implied moving users across farms or versions of farms. I never thought it would be migrate the user across domains. So I had not given it any more thought. The MigrateUser command is working perfectly so far.

    Thanks again.

  3. After importing the functions and trying to run the code for generating reports for:

    Get-SPSite -Limit All | ForEach-Object {
    $site = $_
    $webPermissions += $site | Get-SPWeb | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)
    $listPermissions += $site | Get-SPWeb | %{$_.Lists | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)}
    $itemPermissions += $site | Get-SPWeb | %{$_.Lists | %{$_.Items | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)}}
    $webPermissions + $listPermissions + $itemPermissions | Out-GridView

    I receive an error: Exception calling “GetUserEffectivePermissionInfo” with “1” argument9s): “Attempted to perform an unauthorized operation”
    $perminfo=$so.GetUserEffectivePermissionINfo <<<< ($loginName)

    Would you be able to provide me with some assistance with this error?

  4. This is one of the best posts on the subject I have seen so thanks for that.

    I have one question though, is it possible to restrict this to output certain permissions only, specifically excluding READ and LIMITED ACCESS from the report.

    Our profiles database contains about 8,000 accounts all with read access or “Style Resource Reader” somewhere. This means that “$site.RootWeb.SiteUsers” is typically quite huge and the output not very helpful.

    I’m trying to run a report to list all webs and users with edit/owner permissions (full control/contribute). If I could get this to only show those two permissions I’m there I think.

    I want to something like this:
    $webPermissions = $site | Get-SPWeb –Limit All | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName) [*but don’t output if is Read or Limited Access / Only output if full control or contribute*]]

    This may be a simple question but I am a bit of a powershell novice.

    Thanks again.

  5. It’s not very elegant but I achieved what I wanted. I added an if statement to the permissions for loop to flag items that include the full control permission. This flag is then used later to decide whether to add the role assignment to the hash table and output.

    if ($definition.Name -eq “Full Control”) {
    $permName += $definition.Name
    # Set flag to say found “Full Control” so add this item to the output


  6. How can I limit this to a set selection of resources or subsites. Also is there a way to batch this out to call the initial sharepoint powershell and then to call an additional script from that? Right now I have a batch file that runs:

    C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -NoExit ” & ‘ C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 ‘ ”

    from there I run
    cd \
    . .\SecurityReport.ps1
    $gc = Start-SPAssignment
    $site = $gc | Get-SPSite http://portal/
    $site | Get-SPWeb –Limit All | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName) | Export-Csv c:\perms.csv
    $gc | Stop-SPAssignment

    which all works but I want to make it a single click for a non-powershell person and to limit which subsites are derived, http://portal/site1 and http://portal/site2 for example.

    1. Very good article.
      could you please guide me,how would get the list of details who have permission on any object(web,list) when permission are configured through spgroup.

  7. First – thanks for this script. There looks to be a bug when determining the URL of a SPListItem. The line that reads “$resource = $so.ParentList.ParentWeb.Site.MakeFullUrl($so.Url)” does not work for items in nested webs. This is due to the $so.Url being a site-relative Url, not a server relative one (which is what “MakeFullUrl” expects. As a workaround, replace the line with ” $resource = $so[“EncodedAbsUrl”] ” .

    Thanks again for this script. Saved quite a bit of time.

  8. Folders aren’t included in this. From the sp-list documentation: “The Items property returns all the files in a document library, including files in subfolders, but not the folders themselves. In a document library, folders are not considered items. “.

    So you need another line with
    $folderPermissions = $site | Get-SPWeb –Limit All | %{$_.Lists | %{$_.Folders | Get-SPUserEffectivePermissions ($site.RootWeb.SiteUsers | select LoginName)}}

    Add $folderPermissions to $itemPermissions and all the rest and you’re good.

        1. You wouldn’t insert it into the function. The example Bill provided is how you would call the function passing in the folders which are securable objects (so it’s just another example usage of the function to catch this additional case). It’s perhaps most relevant with my last examples where I “piece it all together”.

  9. Nice script Gary! I tried running the “stitch” script on my test farm (~20 site collection, ~800 subsites, +500 mysites)… it never finished. I left it running over 24 hrs and still didn’t return anything. It works if I run the web or list only scripts.

    Any idea?

  10. Nice script..i am looking for doing the same thing but using c# language and not to convert these lines of code to c# ?…i want to show report on a sharepoint application page..thank you

    1. Get rid of the dollar signs, add type information, replace piping with appropriate loops or functions. Sorry, can’t be more specific without rewriting the code for you 🙂

  11. I’m totally new to powershell so this question might sound stupid, but do i have to enter a username or spsiteurl or something to get the maincode to work?

    1. Thanks for the heads up. I’ve sent the guy an email and hopefully he does the right thing. Unfortunately codeplex’s process for getting copyright violators shut down is less than ideal so I’m hoping I don’t have to go that route.

  12. Hi Gary,

    This is great – thanks so much for this.

    I am using SharePoint 2013 with claims authentication, and I couldn’t get the first example to work without appending “”i:0#.w|” to the user name. (in other words, user name was ‘”i:0#.w|domain\username” instead of “domain\username.”

    I’m trying to convert the login names in the last example to the same, but can’t seem to figure out where to insert the “i:0#.w|” . I tried several permutations of the following, but I am getting various errors, from “You must specify a valid user object” to “user not found” to one indicating it couldn’t cast from a string to an object.

    WebPermissions += $site | Get-SPWeb –Limit All | Get-SPUserEffectivePermissions (Get-SPUser -Identity ("i:0#.w|" + ($site.RootWeb.SiteUsers | select LoginName)))

    Can you provide any help? Thanks!

  13. I have tried it and it ran without error but it gives blank output in outgrid view and export csv both.

    1. It was likely due to a claims encoding of the user name issue. I’ve updated the code with a version I posted on gist – this version will encode the username if the web application supports claims.

  14. Hi Gary,

    First, thank you for this script!
    I am writing my own big script that will get all permissions for a user, starting from Farm,WebApp,Zones,Sites,webs,lists,folders and items. See sample output

    The script should work in SP2010,2013, win classic and win claims.
    My question is (I am sure you can give me definite answer to that ) if the method GetUserEffectivePermissionInfo can get permissions that user inherits from Domain groups?

    I already can get group membership for the user, if this method cannot get the permissions that are inherited I will just include checking for each group, but it will make the script a bit slow.


    1. I think I found the answer myself, the issue is that the permissions that are assigned via domain groups will not appear if the user have never accessed the site, this really bug me out when I tested the scrip

  15. Hello Gary, I was trying to reuse your script in SHP 2013, however i’m getting this error:
    Get-SPUserEffectivePermissions : The term ‘Get-SPUserEffectivePermissions’ is not recognized as
    the name of a cmdlet, function, script file, or operable program. Check the spelling of the name,
    or if a path was included, verify that the path is correct and try again.
    At line:1 char:60

    I’ve checked MSDN and this method still should be there… do you have any idea, please?

Comments are closed.