PowerShell Script To Get All Site Collection, Document Library And Items Of A Particular Web Application

PowerShell scripts is a wonderful command-line scripting language that is associated with .NET framework and .NET core. which can be used for multiple automation and doing recursive processes. It is mainly used by administrators and developers for managing/adminstration of any Microsoft product. [sql,sharepoint,ms-word etc] any software that is running in Windows operating system.
To get started with PowerShell, we need to know the basic cmdlet [commands], syntax, pipeline and use of help command. If we are able to understand and use it effectivitely, we can write any script to get the desired results.
get-cmdlets --> gives the list of all cmdlets
$var --> is used to store values/object in PS
| --> Pipe symbol is used to push the values of one command over other. For ex - Get-SPwebapplication -Identity "url" | Get-SPsite, it will be very useful while looping through objects.

get-help --> cmdlet is used to get help for any command that we would like to know.
We can use alias help *spweb*  - this will return all the cmdlets where spweb word is used. Normally, it used in this format help 'verb/'noun'.
PS script can be run in command-promt or Windows PowerShell ISE which is by default present in Windows operating system from Windows 7 onward.

In-order to use PS, we need to add the particular Snapins, using the below command.
  1. Add-PSSnapin microsoft.sharepoint.powershell    
Once it is added, we can use all the PS commands for SharePoint. The below line gets a web application's site collection and subsites of all site collections. Also, it loops through each list of base template "Document Library" [basically checking all document library] and writes out the count of items.
  1. $SPWebApp =  Get-SPWebApplication -Identity "Url of the web applicaiton"    
  3. #then we can pipe the web application object to get all Site collection    
  5. $SCall = $SPWebApp | Get-SPsite -Limit All    
  7. #this command will get all site collection present in the web application and storing it in a PS object so that we can loop it to get all subsite present in site collection    
  9. foreach($sitecollection in $SCall )    
  10. {    
  11.     $SCwithSubsite = $SC.AllWebs     
  12.       foreach($site in $SCwithSubsite )    
  13.        {    
  14.            foreach($list in $site.Lists)    
  15.           {    
  16.               if($list.BaseTemplate -eq "DocumentLibrary")    
  17.                 {    
  18.                       $NumberOfDoc= $list.ItemCount; #Number item present in document library  
  21.                      #To get more details regarding each item in document library we need to loop through each item "$list.Item"  
  22.                   }  
  23.                   Write-Host "Number of Document present in $list.Title : $NumberOfDoc"  
  24.             }  
  25.          }  
  26. }  
The above PS script helps in getting the number of documents present in each document library of a site.