Powershell – SharePoint document library files report filtered by created date

Powershell – SharePoint document library files report filtered by created date
1 vote, 5.00 avg. rating (95% score)

Introduction

Give the date range and this powershell script will generate csv report with total number of files created in all the document libraries. Script will get all the document libraries of sites and its subsites. This can be used as reporting tool to get overview of the total number of documents that were created in specific time duration

What are covered in this script

  • Navigates each website and its subsite
  • CAML query in powershell with date range
  • Filter only document library
  • Queries documents with in folders also (scope is Recursive)
  • Output as CSV file

Document libraries created file report


Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

function Get-DocReport([string]$siteUrl)
{
	$site = New-Object Microsoft.SharePoint.SPSite $siteUrl
	foreach ($web in $site.AllWebs)
	{
		write-host "Processing for Web :"  $web.Url
		foreach ($list in $web.Lists)
		{
		  if( ($list.BaseType -eq “DocumentLibrary”) -and ([int]$list.BaseTemplate -eq 101))
		   {
		     $spqQuery = New-Object Microsoft.SharePoint.SPQuery
	             $spqQuery.Query = "<Where>
					  <And>
					    <Geq>
					      <FieldRef Name='Created' />
					      <Value IncludeTimeValue='TRUE' Type='DateTime'>2015-01-01T00:00:01Z</Value>
					    </Geq>
					    <Leq>
					       <FieldRef Name='Created' />
					       <Value IncludeTimeValue='TRUE' Type='DateTime'>2015-04-01T00:00:01Z</Value>
					    </Leq>
					 </And>
					</Where>"
			    $spqQuery.ViewFields = ""
			    $spqQuery.ViewFieldsOnly = $true
			    $spqQuery.viewAttributes = "Scope='Recursive'"
			    $splListItems = $list.GetItems($spqQuery)
				   $data = @{
								"Site"      = $site.Url
								"Web"       = $web.Url
								"list"      = $list.Title
								"itemcount" = $splListItems.Count
						   }
			    New-Object PSObject -Property $data
		   }
		}
		$web.Dispose();
	}

	$site.Dispose()
}

try
{
	Get-DocReport "http://adidevsite/siteA" | Export-Csv -NoTypeInformation -Path "C:\Adi\Report\filegrowthreport.csv"
}
catch
{
	write-host $_.exception
}

Script will navigate to all document libraries in sitecollection and subsites in the sitecollection. Report will be generated in the below format

Site Web list itemcount
http://adidevsite/siteA http://adidevsite/siteA MyDocs 10
http://adidevsite/siteA http://adidevsite/siteA SharedDocs 2
http://adidevsite/siteA http://adidevsite/siteA/siteB TeamBDocs 5
http://adidevsite/siteA http://adidevsite/siteA/siteB TeamBSharedDocs 1
http://adidevsite/siteA http://adidevsite/siteA/siteB/siteC TeamCMainDocs 20
http://adidevsite/siteA http://adidevsite/siteA/siteB/siteC TeamCShareDocs 0

Conclusion

This script can be used as utility tool to navigate all SharePoint document libraries from powershell script

Refer for SharePoint Site Template id’s (SPListTemplateType)
https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisttemplatetype.aspx

October 4, 2015 · Adi · No Comments
Tags: , , ,  · Posted in: Powershell, Sharepoint 2010, SharePoint 2013, Tools

Leave a Reply

What is 13 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)