Powershell Script to List out the Subsites, Lists, Item, Item Version Counts and Security for a SharePoint Site Collection

This script captures the below information's,

  1. All subsites under the site collection
  2. All lists
  3. Item Count
  4. All items
  5. Ersion details
  6. Security details

The output will be available as a CSV file and text files on the same location where the script is placed.

  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\SiteReportPatch-$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.  
  15. #Deleting any .rtf files in the scriptbase location  
  16. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  17. if($FindRTFFile)  
  18. {  
  19.   foreach($file in $FindRTFFile)  
  20.   {  
  21.    remove-item $file  
  22.   }  
  23. }  
Start-transcript $logfile 
  1. write-host "################################################" -fore cyan  
  2. Write-host "Generating site reports" -fore yellow  
  3. write-host "Following informations will be captured" -fore yellow  
  4. write-host "All subsites under the site collection" -fore yellow  
  5. write-host "All lists" -fore yellow  
  6. write-host "Item Count" -fore yellow  
  7. write-host "All items" -fore yellow  
  8. write-host "Version details" -fore yellow  
  9. write-host "Security details" -fore yellow  
  10. write-host "################################################" -fore cyan  
  11.   
  12.   
  13. Function SiteReport()  
  14. {  
  15.   
  16.  $Output = $scriptBase + "\" + "SiteReport.csv";  
  17.  "SiteCollection" + "," + "Webs" + "," + "Lists" + "," + "ItemCount" + "," + "Items" + "," + " ItemID"  + "," + "VersionCount" | Out-File -Encoding Default -FilePath $Output;  
  18.  $NoValue = ""  
  19.  $SiteCollectionURL = read-host "Enter the site collection URL "  
  20.  $global:SiteCollectionURL + "," + $NoValue + "," + $NoValue + "," + $NoValue + "," + $NoValue + "," + $NoValue + "," + $NoValue | Out-File -Encoding Default  -Append -FilePath $Output;  
  21.  $global:Site = get-spsite $SiteCollectionURL  
  22.  foreach($web in $site.allwebs)  
  23.  {  
  24.      $NoValue + "," + $web.url + "," + $NoValue + "," + $NoValue + "," + $NoValue + "," + $NoValue + "," + $NoValue | Out-File -Encoding Default  -Append -FilePath $Output;  
  25.      $lists = $web.lists  
  26.      foreach($list in $lists)  
  27.      {  
  28.          $NoValue + "," + $NoValue + "," + $list.title + "," + $list.items.count + "," + $NoValue + "," + $NoValue + "," + $NoValue | Out-File -Encoding Default  -Append -FilePath $Output;  
  29.          $listitems = $list.items  
  30.          foreach($item in $listitems)  
  31.          {  
  32.              if($item.versions)  
  33.              {  
  34.                  $VersionCount = $item.versions.count  
  35.              }  
  36.              else  
  37.              {  
  38.                  $VersionCount = $NoValue  
  39.              }  
  40.              $NoValue + "," + $NoValue + "," + $NoValue + "," + $NoValue + "," + $item.name + "," + $item.ID + "," + $VersionCount | Out-File -Encoding Default  -Append -FilePath $Output;  
  41.          }  
  42.      }  
  43.  }  
  44.  write-host "Site report completed" -fore green  
  45.   
  46. }  
  47.   
  48. Function SitePermission()  
  49. {  
  50.   
  51.  foreach($web in $site.allwebs)  
  52.  {  
  53.    Write-host "Collecting permission details for the web " $web.url -fore Yellow  
  54.    $users = get-spuser -web $web.url -Limit ALL  
  55.    foreach($user in $users)  
  56.    {  
  57.        write-host $User.userlogin  
  58.        $permissionInfo = $web.GetUserEffectivePermissionInfo($User.userLogin)  
  59.        $roles = $permissionInfo.RoleAssignments  
  60.        write-host "Now checking the permissions of the user "  $User.userLogin " in the site " $web.Url -fore magenta  
  61.        for ($i = 0; $i -lt $roles.Count; $i++)  
  62.        {  
  63.            $bRoles = $roles[$i].RoleDefinitionBindings  
  64.            foreach ($roleDefinition in $bRoles)  
  65.            {  
  66.                if ($roles[$i].Member.ToString().Contains('\'))  
  67.                {  
  68.                    write-host "The User "  $user.userLogin  " has direct permissions "  $roleDefinition.Name -fore green  
  69.        "The User " + $user.userLogin + " has direct permissions "  + $roleDefinition.Name | out-file $scriptbase\DirectPermissons.txt -append  
  70.                }  
  71.                else  
  72.                {  
  73.                    write-host "The User "  $user.userLogin  " has permissions "  $roleDefinition.Name  " given via "  $roles[$i].Member.ToString() -fore green  
  74.        "The User "  + $user.userLogin  + " has permissions "  + $roleDefinition.Name + " given via " + $roles[$i].Member.ToString() | out-file $scriptbase\GroupPermissions.txt -append  
  75.                   }    
  76.               }    
  77.           }    
  78.       }  
  79.   
  80.      }  
  81.      write-host "Permission details collected" -fore green  
  82. }