UPDATE 1:
I have the following script, but it's not working correctly:
Add-PSSnapin Microsoft.SharePoint.PowerShell
#input = https://sharepoint.oshirowanen.com/sites/oshdev
$websiteurl = read-host "Enter site URL"
$web = Get-SPWeb $websiteurl
#input = library one
$LibraryName = read-host "Enter Library Name"
$docLibrary = $web.Lists[$LibraryName]
#input = https://sharepoint.oshirowanen.com/sites/oshdev/library one/user one
$SourceFolderURL = read-host "Enter the source folder URL"
#input = https://sharepoint.oshirowanen.com/sites/oshdev/library two/user one
$TargetFolderURL = read-host "Enter the target folder URL"
[string]$CurrDir=$args[0]
$logfile = $CurrDir + "\log.log"
$errorActionPreference = 'SilentlyContinue'
Function ProcessFolder([string]$sourcefolder,[string]$targetfolder)
{
write-host "--------------------------------------------------"
write-host "START OF FUNCTION:"
write-host "SOURCE:" $sourcefolder
write-host "DESTINATION:" $targetfolder
$SFolder = $web.GetFolder($sourcefolder)
$TFolder = $web.GetFolder($targetfolder)
$Sfoldername = $SFolder.name
$tfoldername = $TFolder.name
foreach ($file in $SFolder.Files)
{
try
{
write-host "--------------------------------------------------"
write-host "LOOP 1:"
write-host "DESTINATION: $targetfolder/$file.Name"
write-host "CopyTo: $targetfolder/$file.Name"
$file.CopyTo("$targetfolder/"+$file.Name,$true)
[Microsoft.SharePoint.SPFile]$spFile = $web.GetFile("$targetfolder" + "/" + $file.Name)
}
# Display and log the filename which is failed to copy to destination folder
catch
{
$fname = $file.Name
Write-Error 'An error occured while copying the file '$fname 'from Folder' $sourcefolder
}
$sourceitem = $file.item
$targetitem = $spFile.item
if($spFile.Exists -eq $true)
{
try
{
$targetitem["Author"] = $sourceitem["Author"]
$targetitem["Editor"] = $sourceitem["Editor"]
$targetitem["Created"] = $sourceitem["Created"]
$targetitem["Modified"] =$sourceitem["Modified"]
$targetitem.Update()
}
# Display and log the filename which is failed to update metadata destination folder
catch
{
$fname = $file.Name
Write-Error 'An error occured while updating the metada fields for the file' $fname 'from Folder' $sourcefolder 'as the user does not exists '
}
}
}
foreach ($subfolder in $SFolder.SubFolders)
{
$subfoldername = $subfolder.name
write-host "--------------------------------------------------"
write-host "LOOP 2:"
write-host "TFolder:" $TFolder.URL
write-host "subfoldername:" $subfoldername
$folderobj = $docLibrary.AddItem($TFolder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "$subfoldername")
$folderobj.Update();
$targetfolderurl = $folderobj.URL
#write-host $folderobj.URL
$strTempDestination = $websiteurl + "/" + $subfolder
$strNewDestination = $strTempDestination.replace("/library one/","/library two/")
write-host "SOURCE: $websiteurl/$subfolder"
write-host "DESTINATION: $strNewDestination"
ProcessFolder "$websiteurl/$subfolder" "$strNewDestination"
}
}
write-host "--------------------------------------------------"
write-host "START:"
write-host "SOURCE:" $SourceFolderURL
write-host "DESTINATION:" $TargetFolderURL
write-host "--------------------------------------------------"
ProcessFolder "$SourceFolderURL" "$TargetFolderURL"
Remove-PsSnapin Microsoft.SharePoint.PowerShell
It does not give any errors, but it does not create the user one
folder in library two
and it only copies the files in the root of library one\user one
to library two
, and it doesn't copy the folders, only the files in the root.
I've tried finding the error by using write-host
in the hope that i might see what goes wrong, but the source and destination paths look fine to me.
Any ideas?
ORIGINAL QUESTION:
I know how to move an entire document library to a different document library using powershell export-spweb and import-spweb. But how do I move a specific folder within a document library to a specific folder within a different document library?
At the same time retaining the version history and meta data of any files moved.
Using SharePoint 2010.