Powershell – Move files between document libraries in the same Site Collection

powershellsharepointsharepoint-2007

I have a need to move ~10,000 files from one document library to another in the same site collection. I believe that powershell is the best means for doing this.

I found the following article: http://blog.isaacblum.com/2011/10/04/spfilecollection-class-copy-files-to-another-document-library/#respond that suggested a means for doing this however I'm unsure how to adapt this script (I'm getting my first exposure to Powershell with this project).

I tried the following to no avail:

$PSSnapin = Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
clear

$org = "hhttp://farm/sitecollection/Document Library Source/Forms/AllItems.aspx"
$dest = "hhttp://farm/sitecollection/Document Library Destination/Forms/AllItems.aspx"

$orgLibrary = (Get-SPWeb $org).Folders["Documents"]
$destLibrary = (Get-SPWeb $dest).Folders["Documents"]
$destFiles = $destLibrary.Files
foreach ($file in $orgLibrary.Files)
{
    $curFile = $file.OpenBinary()
    $destURL = $destFiles.Folder.Url + "/" + $file.Name
    $destFiles.Add($destURL, $curFile, $true)
}

Is there an alternate way of doing this? Note that I'm using MOSS2007 and Powershell 2.0, not SharePoint 2010.

Update / Semi-Answer:

As per x0n's post below this isn't supported in SharePoint 2007 (only 2010). I recevied the following advice outside of this thread, which is pertinent, and should help others in future:

Unfortunately SharePoint 2010's Management Shell (it's PowerShell
snap-in and associated cmdlets) is not compatible with MOSS 2007 and
there aren't cmdlets available directly from Microsoft for that
version of SharePoint. What that means is that you can still use
PowerShell with MOSS 2007, but you're either going to have to write
your own cmdlets that use STSADM or the SharePoint Object Model
directly, or you're going to have to use MOSS 2007-compatible cmdlets
from a third party. I'd suggest checking out Gary Lapointe's blog for
a lot of great PowerShell cmdlets for MOSS 2007
(http://blog.falchionconsulting.com/), or places where people upload
cmdlets such as CodePlex.com, the TechNet Script Repository,
POSHCode.org, or http://get-spscripts.com/.

Best Answer

I'm not surprised you're getting nowhere: The Microsoft.SharePoint.PowerShell snapin is for SharePoint 2010 only, and isn't available on a SharePoint 2007 server.

Frankly, the easiest way to do this is to open Internet Explorer, navigate to the source document library and open the "explorer view." Select all documents, and copy (ctrl+c). Open another IE window and do the same thing for the target document library and paste (ctrl+v).

If it won't open in explorer view, ensure that the machine you're using to do the copy/paste has the "WebClient" service running. If you're running Windows 2008 R2, this service is not available unless you decide to add the "desktop experience" feature. It's a lot easier to find a Windows 7 machine instead, which will have the WebClient service (but be sure it's running.)

Update:

That said, your script is probably about 80% there and doesn't really need that 2010 snapin. I can't test this right now (sorry) but it should be about 99% correct:

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

$org = "http://farm/sitecollection/sourcedoclib" 
$dest = "http://farm/sitecollection/targetdoclib" 

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

$srcLibrary = $web.Lists["sourcedoclib"] 
$destLibrary = $web.Lists["targetdoclib"] 

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

foreach ($item in $srcLibrary.Items) 
{ 
   if ($item.File) {
        $curFile = $item.file.OpenBinary() 
        $destURL = $destFiles.Folder.Url + "/" + $item.file.Name 
        $destFiles.Add($destURL, $curFile, $true)
    }
} 

Good luck.

Related Topic