Powershell Script to Find Specific user Permission for a SharePoint Site

  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\UserPermissionsDetailsPatch-$LogTime.rtf"  
  3.  
  4. # Add SharePoint PowerShell Snapin  
  5.   
  6.   
  7. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {  
  8.     Add-PSSnapin Microsoft.SharePoint.Powershell  
  9. }  
  10.   
  11. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  12. Set-Location $scriptBase  
  13.  
  14. #Deleting any .rtf files in the scriptbase location  
  15. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  16. if($FindRTFFile)  
  17. {  
  18.  foreach($file in $FindRTFFile)  
  19.   {  
  20.    remove-item $file  
  21.   }  
  22. }  
  23.   
  24. start-transcript $logfile  
  25.   
  26.   
  27. $siteCollection = read-host "Enter the site collection URL "  
  28. $username = read-host "Enter the user name (e.g: domain\username) "  
  29. $site = get-spsite $siteCollection  
  30. foreach($web in $site.allwebs)  
  31. {  
  32.  
  33.    #Check if the user is site collection administrator  
  34.    $getuser = get-spuser -identity $username -web $web.url  
  35.      
  36.    if($getuser.issiteadmin)  
  37.    {  
  38.     write-host "The user " $username " is a site collection administrator" -for green  
  39.    }  
  40.      
  41.    $permissionInfo = $web.GetUserEffectivePermissionInfo($Username)  
  42.    $roles = $permissionInfo.RoleAssignments  
  43.    write-host "Now checking the permissions of the user "  $Username " in the site " $web.Url -fore yellow  
  44.    for ($i = 0; $i -lt $roles.Count; $i++)  
  45.    {  
  46.        $bRoles = $roles[$i].RoleDefinitionBindings  
  47.        foreach ($roleDefinition in $bRoles)  
  48.        {  
  49.            if ($roles[$i].Member.ToString().Contains('\'))  
  50.            {  
  51.                write-host "The User "  $username  " has direct permissions "  $roleDefinition.Name -fore green  
  52.            }  
  53.            else  
  54.            {  
  55.                write-host "The User "  $username  " has permissions "  $roleDefinition.Name  " given via "  $roles[$i].Member.ToString() -fore green  
  56.            }  
  57.   
  58.   
  59.        }  
  60.   
  61.   
  62.    }  
  63.      
  64.   
  65.   }