Powershell Script For Getting The Items Count Which Are Added In Last 30 Days

  1.  <#  
  2. .SYNOPSIS  
  3. Can be used to loop through all lists available across the site collection, from all sites and their item counts which are added in last 30 days within SharePoint Online tenant. We are writting list title, relative url of list and last 30 days item count in .csv file  
  4.   
  5. .EXAMPLE  
  6. PS C:\> .\Get-Last30DaysItemCountFromAllListInSiteCollection.ps1 -SiteCollectionUrl https://MySiteCollection.sharepoint.com  
  7.   
  8. .EXAMPLE  
  9. PS C:\> $creds = Get-Credential  
  10. PS C:\> .\Get-Last30DaysItemCountFromAllListInSiteCollection.ps1 -SiteCollectionUrl https://contoso-admin.sharepoint.com -Credentials $creds  
  11.  
  12. #>   
  13.   
  14. [CmdletBinding()]  
  15. param  
  16. (  
  17.     [Parameter(Mandatory = $true, HelpMessage="Enter the URL of the target site     collection , e.g. 'https://contoso.sharepoint.com'")]  
  18.     [String]  
  19.     $SiteCollectionUrl,  
  20.   
  21.     [Parameter(Mandatory = $false, HelpMessage="Optional administration credentials to site collection.")]  
  22.     [PSCredential]  
  23.     $Credentials  
  24. )   
  25.  
  26.  #Looping through all the webs and all their sub webs. Calling this function recursively.   
  27.  function Loop-AllWebs{  
  28.   Param(  
  29.         [Microsoft.SharePoint.Client.ClientContext]$Context,  
  30.         [Microsoft.SharePoint.Client.Web]$RootWeb  
  31.         )  
  32.         
  33.         #Getting all webs  
  34.         $Webs = $RootWeb.Webs  
  35.           
  36.         $Context.Load($Webs)  
  37.         $Context.ExecuteQuery()  
  38.         ForEach ($sWeb in $Webs)  
  39.         {  
  40.             Get-AllListsDetails -Context $Context -web $sWeb  
  41.             #Looping through all sub webs  
  42.             Loop-AllWebs -RootWeb $sWeb -Context $Context  
  43.         }  
  44.     }   
  45.   
  46.  function Get-AllListsDetails{  
  47.      Param(  
  48.             [Microsoft.SharePoint.Client.ClientContext]$Context,  
  49.             [Microsoft.SharePoint.Client.Web]$web  
  50.             )  
  51.   
  52.             $lists=$web.Lists  
  53.             $Context.Load($lists)  
  54.             $Context.ExecuteQuery()  
  55.             foreach($list in $lists)  
  56.              {  
  57.                 #Getting view for getting the list URL  
  58.                 $view = $list.DefaultView  
  59.   
  60.                 $spQuery = New-Object Microsoft.SharePoint.Client.CamlQuery  
  61.                 #CAML Query Using a DateTime Value and and Offset of Today  
  62.                 $query = '<View><Query><Where><Geq><FieldRef Name="Created" /><Value Type="DateTime"><Today OffsetDays="-30" /></Value></Geq></Where></Query></View>'  
  63.                 $spQuery.ViewXml = $query  
  64.                 $spListItemCol = $list.GetItems($spQuery)  
  65.   
  66.                 $Context.Load($list)  
  67.                 $Context.Load($view)  
  68.                 $Context.Load($spListItemCol)  
  69.                 $Context.ExecuteQuery()  
  70.          
  71.                 $line = $list.Title + "," + $view.ServerRelativeUrl + "," + $spListItemCol.Count  
  72.                 #Writting to CSV file  
  73.                 Add-Content Result.csv $line #Give the full path where you want to store the result. Currently it will be generated at the location of this script  
  74.    }   
  75.  }   
  76.  
  77.  # Get reference to SPO CSOM assemblies  
  78. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
  79. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"   
  80.   
  81.  Write-Host -ForegroundColor White "---------------------------------------------------------------------------"  
  82. Write-Host -ForegroundColor White "Get All Lists across Site Collection and last 30 days Items counts from each and every lists "  
  83. Write-Host -ForegroundColor White "---------------------------------------------------------------------------"  
  84.   
  85. Write-Host -ForegroundColor Yellow "Site Collection URL: $SiteCollectionUrl"  
  86. Write-Host ""  
  87. Write-Host ""   
  88.  
  89. # Get credentials, if they were not provided  
  90. if($Credentials -eq $null)  
  91. {  
  92.     $Credentials = Get-Credential -Message "Enter Admin Credentials"  
  93. }   
  94.   
  95. [Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollectionUrl)   
  96.   
  97. $clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName, $Credentials.Password)   
  98.   
  99. $web = $clientContext.Web  
  100.    
  101. $clientContext.Load($web)  
  102. $clientContext.ExecuteQuery()  
  103.   
  104. Loop-AllWebs -RootWeb $web -Context $clientContext