Quick note: First and foremost - this is somewhat in line with a previous post of mine found here: https://stackoverflow.com/questions/11514833/move-files-between-document-libraries-in-the-same-site-collection however I've opened a new question for this as I didn't want to update my original post and destroy the value to be gained there for future readers as this is, in essence, a spin-off question of my original one.

Goal I have a number of document libraries (100+) that have content in them that need to be archived for the end of financial year. Essentially content will move from the source document library, to the root of the destination library.

Current Code

[reflection.assembly]::loadwithpartialname("microsoft.sharepoint") > $null

$org = "http://dev/re/ad/wf/Approval%20History%20%20Centre%20EV" 
$dest = "http://dev/re/ad/wf/Approval%20History%20%20Centre%20EV1" 

$site = new-object microsoft.sharepoint.spsite $org
$web = $site.openweb()

$srcLibrary = $web.Lists["Approval History - Centre EV"] 
$destLibrary = $web.Lists["Approval History - Centre EV1"] 

$destFiles = $destLibrary.Folders["Approval History - Centre EV"]

write-host "srcLibrary is: $srcLibrary"
write-host "destLibrary is: $destLibrary"
write-host "destFiles is: $destFiles"

foreach ($item in $srcLibrary.Items) 
   if ($item.File) 
        $curFile = $item.file.OpenBinary() 
        $destURL = $dest + "/" + $item.file.Name 

        write-host "destURL is: $destURL"
        write-host "srcLibrary is: $srcLibrary"

        $destFiles.Add($destURL, $curFile, $true)

Error and output

I'm currently seeing the following output:

srcLibrary is: Approval History - Centre EV
destLibrary is: Approval History - Centre EV1
destFiles is: 
destURL is: http://dev/re/ad/wf/Approval%20History%20%20Centre%20EV1/EV_00249629__2011-03-15T095937077.xml
srcLibrary is: Approval History - Centre EV
You cannot call a method on a null-valued expression.
At line:30 char:23
+         $destFiles.Add <<<< ($destURL, $curFile, $true)
    + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Code Issue

Currently $destLibrary in the code above is assigning to a null value - unfortunately I lack the expertise to understand why. I've also tried creating a folder named 'Archived' in the destination library and setting this line to the following:

$destFiles = $destLibrary.Folders["Archived"]

Unfortunately this results in the same output as shown above.

A bit about the farm

Server name: dev
Site Collection: /re/
Site: /wf/, a sub-site of /ad/
  • What's the output of: $destLibrary.Folders | gm and $destLibrary.Folders | fl * after you've set the $destLibrary value?
    – Chris N
    Jul 19, 2012 at 18:28

2 Answers 2


Try this,

// Get your source and destination libraries
SPList source = web.GetList(web.ServerRelativeUrl + @"/SourceDocLib");
SPList destination = web.GetList(web.ServerRelativeUrl + @"/DestinationDocLib");

// Get the collection of items to move, use source.GetItems(SPQuery) if you want a subset
SPListItemCollection items = items = source.Items;

// Get the root folder of the destination we'll use this to add the files
SPFolder folder = web.GetFolder(destination.RootFolder.Url);

// Now to move the files and the metadata
foreach (SPListItem item in items)
    //Get the file associated with the item     
    SPFile file = item.File;
    // Create a new file in the destination library with the same properties
    SPFile newFile = folder.Files.Add(folder.Url + "/" +file.Name, file.OpenBinary(),file.Properties,true);
    // Optionally copy across the created/modified metadata                        
    SPListItem newItem = newFile.Item;
    newItem["Editor"] = item["Editor"];
    newItem["Modified"] = item["Modified"];
    newItem["Author"] = item["Author"];
    newItem["Created"] = item["Created"];

    // UpdateOverwriteVersion() will preserve the metadata added above. 

If you want to delete the originals as well use for (int i = items.Count -1; i > -1 ; i--) instead of the foreach to iterate backwards through the collection and items[i].Recycle(); or items[i].Delete(); to recycle or delete the original after you have moved it.

  • @aquilla sands I wanted to delete the originals as well, i tried for loop but didnt work.
    – George
    Apr 9, 2020 at 10:25

try something like :

$web = "http://dev/re/ad/wf/"

$orgLibrary = (Get-SPWeb $web).Folders["Approval History - Centre EV"]
$destLibrary = (Get-SPWeb $web).Folders["Approval History - Centre EV1"]

$destFiles = $destLibrary.Files
foreach ($file in $orgLibrary.Files)
    $curFile = $file.OpenBinary()
    $destURL = $destFiles.Folder.Url + "/" + $file.Name
    $destFiles.Add($destURL, $curFile, $true)


  • I quoted this one in my original posting :) The premise of this post is to find a way to do this in MOSS, this script only supports Sharepoint 2010... thanks anyway!
    – Michael A
    Jul 18, 2012 at 9:08
  • On 2007, try to use the extended stsadm by gary lapointe : blog.falchionconsulting.com/index.php/2008/05/…
    – Le_Fredo
    Jul 19, 2012 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.