Wednesday, October 29, 2014

Archive SharePoint Library Files into Sub-Folders using PowerShell

Requirement: We've a very large library with more than 20,000 files stored in the root of the document library without any sub-folders. Got to move files into Sub-folders to improve performance.

Solution: We analyzed the library content and decided to move files into sub-folders based on file's created date.

PowerShell script to move files into sub-folders in SharePoint document library:

#Import PowerShell Snap-in
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

#Create a custom function to archive files
Function Archive-Files($WebURL, $ListName)
 #Get the web and List
 $Web = Get-SPWeb $WebURL
 $List= $web.Lists[$ListName]

 $RootItems = $List.RootFolder.files 
 Write-host "Total number of files found in the Root: "$RootItems.count

 #Move each file in the root folder to archive folder
 foreach($item in $RootItems)
   #Get the File
   $file = $Web.GetFile($item.URL)

   #Get the Month & Year values from the File's crated date
   $MonthYearValue = $file.TimeCreated.ToString('MMM-yyyy')
   # Try to Get the Archive Sub-Folder in the target Library
   $TargetFolder = $List.ParentWeb.GetFolder($List.RootFolder.Url + "/" +$MonthYearValue);
   #If the Archive folder doesn't exist, Create it!
   if ($TargetFolder.Exists -eq $false)
       $TargetFolder = $List.Folders.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $MonthYearValue)
    #Move the File
    $file.MoveTo($TargetFolder.Url + "/" + $  

#Call the function to Archive Files
Archive-Files "" "Invocies"
This script preserves Metadata, BTW! You can also move files into sub-folders of an another SharePoint library for archival.

Move List Items to Sub-Folders using PowerShell:
In an another scenario, had to move List items to Sub-folders. Here is the script:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$WebURL = ""
$ListName ="News List"

#Get site object
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]
$TargetFolder = $Web.ServerRelativeUrl + "/" + $List.RootFolder.Url + "/"+ "2014/Jan"

#Get all Files Created specific time frame
$Query = '<Where><And><Geq><FieldRef Name="Date" /><Value Type="DateTime">2014-01-01T00:00:00Z</Value></Geq><Leq><FieldRef Name="Date" /><Value Type="DateTime">2014-01-31T00:00:00Z</Value> </Leq> </And> </Where>'
$SPQuery = new-object Microsoft.SharePoint.SPQuery
$SPQuery.ViewAttributes = "Scope='Recursive'" #To include Sub-folders in the library
$SPQuery.Query = $Query
$ItemCollection =$List.GetItems($SPQuery)

Foreach($Item in $ItemCollection)
    write-host "Moved Item:"$Item.ID

Write-host "Total Items Moved:"$ItemCollection.count

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

1 comment :

  1. Been searching for days now!

    This line saved me and my timeline: $file.MoveTo($TargetFolder.Url + "/" + $

    Thank you so much!

    Note: you have to have the same library structure to be able to maintain meta-data


Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...