I am trying to copy documents from a document library to another document library in a subsite.

I have this script

write-host "Starting to copy" -ForegroundColor Cyan 

#Add the SharePoint snapin
Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue

write-host "Loading powershell module" -ForegroundColor Cyan    
$urlWebSrc = "http://mysite"
$urlWebTgt = "http://mysite/Search"
$listNameSrc = "Documents"
$listNameTgt = "Archieve"

$webSrc = Get-SPWeb $urlWebSrc
$webTgt = Get-SPWeb $urlWebTgt

# Get your source and destination libraries
$source = $webSrc.Lists[$listNameSrc]
$destination = $webTgt.Lists[$listNameTgt]

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

# Get the root folder of the destination we'll use this to add the files
$folder = $webSrc.GetFolder($destination.RootFolder.Url);

# Now to move the files and the metadata
foreach ($item in $items)
    #Get the file associated with the item     
    $file = $item.File;

    $newFileDest = ("{0}/{1}/{2}" -f $urlWebTgt, $folder.Url, $file.Name);
    write-host "moving to" - $newFileDest;

    $item.File.MoveTo($newFileDest, $true)                           

But this is not working. I am getting errors:

Exception calling "MoveTo" with "2" argument(s): "Folder "2006Archieve" does not exist." At C:\script\archive2.ps1:41 char:26 + $item.File.MoveTo <<<< ($newFileDest, $true) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

  • Am assuming there is a folder called "2006Archieve"? – Ransher Singh Jun 3 '15 at 14:46
  • Why don't you use $folderUrl = $destination.RootFolder.Url; instead of trying to retrieve the folder. You only need the url anyway. – Andreas Jun 3 '15 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.