powershell – Move a specific folder in a document library

UPDATE 1:

I have the following script, but it does not work properly:

Add-PSSnapin Microsoft.SharePoint.PowerShell

#input = https://sharepoint.oshirowanen.com/sites/oshdev
$ websiteurl = read-host "Enter the URL of the site"
$ web = Get-SPWeb $ websiteurl

#input = library one
$ LibraryName = read-host "Enter the name of the library"
$ 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 & # 39;

ProcessFolder function ([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)
}
# Displays and saves the name of the file that was not copied to the destination folder
capture
{
$ fname = $ file.name
Write-Error & # 39; An error occurred while copying the file & # 39; $ fname & # 39; from the folder & # 39; $ 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 ()
}
# Displays and logs the name of the file that could not update the destination folder for the metadata
capture
{
$ fname = $ file.name
Write-Error & # 39; An error occurred while updating meta-meta fields of the file & # 39; $ fname & # 39; from the folder & # 39; $ sourcefolder, because the user does not exist & # 39;
}
}

}
foreach (subfolder $ in $ SFolder.SubFolders)
{
$ subfolder = $ subfolder.name

write-host "---------------------------------------------- ---- ---- "
write-host "LOOP 2:"
write-host "TFolder:" $ TFolder.URL
write-host "file_name:" $ file_name

$ folderobj = $ docLibrary.AddItem ($ TFolder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]:: Folder, "$ subfolder")
$ 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 gives no error, but it does not create the user a folder in library two and he only copies files to the root of library a user a at library twoand do not copy the folders, but only the files in the root.

I tried to find the mistake using write-host in the hope of seeing what's wrong, but the source and destination paths fit me well.

Ideas?


ORIGINAL QUESTION:

I know how to move an entire document library to another, using powershell export-spweb and import-spweb. But how to move a specific folder from a document library to a specific folder in another document library?

At the same time, keep the version history and metadata of all the files moved.

Using SharePoint 2010.