Follow Us:
I recently needed to migrate all documents that hadn’t been modified in 90 days in all SharePoint webs within a SharePoint site collection to a records center. There could potentially be hundreds of libraries and content types and creating Information Management Policies at this point wasn’t really feasible. With some good upfront planning, that would have been the preferable method, but this was the quick and easy way to accomplish the task.
This script takes two URLs as parameters and the number of days to compare against the last modified date. The two URLs are the root of the site collection you want to archive documents in, and the other is the path to the destination library where the documents will be moved.
This will loop through all libraries in all webs in a site collection and look for documents whose last modified date is older than 90 days. I then set this up using a scheduled task in Windows Task Scheduler to run on a recurring basis to keep documents in the correct location.
1: #Add SharePoint PowerShell SnapIn if not already added
2: if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
3: Add-PSSnapin "Microsoft.SharePoint.PowerShell" }
4:
5:
6: $siteURL = "http://sitecollectionroot"
7: $destinationURL = "http://path/to/destination/web/including/library"
8:
9: $numDaysToExpire = 90
10: $expireDate = Get-Date
11: $expireDate = $expireDate.AddDays(-$numDaysToExpire)
12:
13: Get-SPSite -Identity $siteURL | Get-SPWeb -limit all | foreach {
14: write-host "Working on Web: "$_.Title -ForegroundColor Green
15: foreach ($list in $_.Lists){
16: if($list.BaseType -eq "DocumentLibrary" -and $List.Hidden -eq $false -and $list.Title -ne "Style Library" -and $list.Title -ne "Master Page Gallery" -and $list.Title -ne "List Template Gallery" -and $list.Title -ne "Theme Gallery" -and $list.Title -ne "Web Part Gallery") {
17: write-host " Working on library: "$list.Title -ForegroundColor Cyan
18: foreach($item in $list.Items){
19: if($item -lt $expireDate){
20: write-host " Moving $($item) with modified date $($item)"
21: $item.File.MoveTo($destinationURL + "/" + $item.Name, $true)
22: }
23: }
24: }
25: } }
You can also get fancy if you want and automatically create folders in the destination based on where it came from. To create a SharePoint folder using PowerShell, you can use this code. This checks to see if the folder exists, and if not, creates it based on the name of the web.
1: $folderName = $destination.Folders | where {$_.Name -eq $_}
2:
3: if(!$folderName)
4: {
5: $newFolder = $destination.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$_)
6: $newFolder.Update()
7: $folderName = $destination.Folders | where {$_.Name -eq $_}
8: }
For more information on C5 Insight or this blog, please Contact Us.
DevelopmentSharePoint, per the error it doesn't have a value somewhere. The error says $item is null. You would have to trace back up and see where the value is null, either from the parent or if statement evaluations.
your code is not working
Jezzy, what problem are you having?
It seems like there are some typos in the code, missing braces and missing letters. Mostly at the beginning of the lines. I'm guessing it is a copy pasta mistake :). (Lines 4 and 13 missing } and G respectively)
AMadKinnon, you're correct, our apologies, I believe I have updated to work correctly. The first character of each line was cutoff, and there were 2 missing close brackets. Doug
Hi, Simply Getting error like below; at 21st line item in your above code. - $item.File.MoveTo($destinationURL + $item.Name, $true) ------------------------------------------------------------------------------------------------------------------------------------------------------ You cannot call a method on a null-valued expression. At C:\Users\spDev\Documents\Archive Closed List-Items in SharePoint with PowerShell.ps1:36 char:9 + $item.File.MoveTo($destinationURL + $item.Name, $true) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull ------------------------------------------------------------------------------------------------------------------------------------------------------ I tried even removing - $item.File.MoveTo($destinationURL + $item.Name) Still the same Issue. What could be the error? please let me know.
The complementary paper includes over 12 years of research, recent survey results, and CRM turnaround success stories.
Request Download
This 60-second assessment is designed to evaluate your organization's collaboration readiness.
Learn how you rank compared to organizations typically in years 1 to 5 of implementation - and which areas to focus on to improve.
This is a sandbox solution which can be activated per site collection to allow you to easily collect feedback from users into a custom Feedback list.
Whether you are upgrading to SharePoint Online, 2010, 2013 or the latest 2016, this checklist contains everything you need to know for a successful transition.