0
votes

I am beginner in sharepoint specially in powershell. I am trying to move all the documents of a document library from one site to another site inside a folder. but I could just copy the files not metadata whcih come from contenttypes!

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


$org = "http://'YYYYY'/sites/XXXX"
$dest = "http://'YYYYY'/sites/XXXX/Subsite"

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

1 Answers

0
votes

This one here does just that. Except for modified and created dates, all other fields are brought over.

$web = Get-SPWeb "http://sharepointed.com/"
$list = $web.Lists["Shared Documents"]

$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'";
$spQuery.RowLimit = 2000
$caml = '<Where><Lt><FieldRef Name="Created" /><Value IncludeTimeValue="TRUE" Type="DateTime">2014-01-01T04:06:45Z</Value></Lt></Where> '
$spQuery.Query = $caml 

do
{
    $listItems = $list.GetItems($spQuery)
    $spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
    $listTotal = $listItems.Count

    for ($x=$listTotal-1;$x -ge 0; $x--)
    { 
        try
            {  
                $listItems[$x].CopyTo("http://sharepoint/Docs/Documents/"+ $listItems[$x].name) 
                Write-Host("DELETED: " + $listItems[$x].name)
                $listItems[$x].Recycle() 
            }
        catch
            {
            Write-Host $_.Exception.ToString()
            }
    }
}
while ($spQuery.ListItemCollectionPosition -ne $null)

via Ian