Getting list items between dates from SharePoint using PowerShell

Getting list items between dates from SharePoint using PowerShell

SharePoint keeps track of changes through the change log. The change log is configured per web application in central administration. Migrator tools like DocAve Migrator make use of this change log to determine which files have been changed during an incremental migration. This change log proofs to be handy when doing migration. Unfortunately i run into a situation where the change log was not sufficient enough. I only could go back for 30 days. Except the last migration was more than 45 days ago.

So i needed something to determine if there were changes during the other 15 days. I wrote a small PowerShell script to identify those changes. The following script identifies all files modified between two dates. For each of the items, the necessary information is stored into a CSV file which can be imported into Excel. The script ignores the workflow related lists and the User Information List.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

$outputPath = "d:scriptscheckedfiles.csv"

$dateFrom = [datetime]("1/22/2016")
$dateTo = [datetime]("3/5/2016")

$siteURL = "http://somesite"
$site = Get-SPSite($siteURL)

Add-Content -Path $outputPath -Value "WebUrl|ListTitle|ListBaseType|ItemName|ItemUrl|ItemModified"

foreach($web in $site.AllWebs) 
{
  foreach($list in $web.Lists) 
  {

      if ($list.Title -ne "Workflows" -and $list.Title -ne "Workflow History" -and $list.Title -ne "User Information List")
       {
      foreach ($listItem in $list.Items)
       {

          $a=[datetime]($listItem["Modified"])


	   $datefromAsString = Get-Date $dateFrom -format d
	   $dateToAsString = Get-Date $dateTo -format d
           $modifiedAsString=Get-Date $a -format d

           Write-Host "from:" $dateFromAsString " to:" $datetoAsString " item:" $modifiedasString

	  if (($a -gt $dateFrom) -and ($a -le $dateTo))
	   {
	       $text = $web.Url + "|" + $list.Title + "|" + $list.BaseType + "|" + $listItem["Title"] + "|" + $listItem.Url + "|" + $modifiedAsString 

               Add-Content -Path $outputPath -Value $text

               Write-Host "Item found between dates and is added to csv file" 
          }
       }
     }

  }

  $web.Dispose();
}

$site.Dispose();

Leave a Reply