SharePoint copy files between document libraries using PowerShell with versioning

This PowerShell SharePoint tutorial, we will discuss, how to copy files between document libraries using PowerShell with versioning in SharePoint 2013/2016.

This PowerShell script we can use to copy files from one document library to another document library with fields and file versions in SharePoint 2013/2016.

SharePoint copy files between document libraries using PowerShell with versioning

Below is the PowerShell script to copy files from one document library to another document library in SharePoint 2013/2016.

cls
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$web = Get-SPWeb -Identity "Site collectgion URL Here"
$org = $web.Lists.TryGetList("TrainingMaterials")
$dest = $web.Lists.TryGetList("TrainingDestination")
$folderName= "Source Folder Name"
$destFolderName = "Destination Fodler Name"

$source = $web.GetList($web.Url + "/"+$org.RootFolder.Url + "/")
$destination= $web.GetList($web.Url + "/"+$dest.RootFolder.Url + "/")
Write-Host $source

$itemsParent = $source.Folders.Item(0)
$itemsub= $itemsParent.Folder.SubFolders
$items= $itemsub.Item(0)
$files= $items.Files
$folder = $web.GetFolder($destination.RootFolder.Url)
Write-Host $folder
foreach ($item in $files)
{
$versionCount=$item.Versions.Count
$dateCreatedOn = $item.TimeCreated.ToLocalTime()
for ($i = 0; $i -le $versionCount; $i++)
{
$strVerComment = "";
$bolMajorVer = $false;
$hashSourceProp = @{}
$userCreatedBy=$item.Author
if ($i -lt $versionCount)
{
$fileSourceVer = $item.Versions.Item($i)
$hashSourceProp = $fileSourceVer.Properties;
$userModifiedBycheck = $fileSourceVer.Properties.vti_modifiedby
$userModifiedBy = $web.EnsureUser($userModifiedBycheck)
$dateModifiedOn = $fileSourceVer.Created.ToLocalTime()
$strVerComment = $fileSourceVer.CheckInComment
$bolMajorVer = $fileSourceVer.VersionLabel.EndsWith("0") -and $true -or $false
$streamFile = $fileSourceVer.OpenBinaryStream()
}
else
{
$userModifiedBy = $item.ModifiedBy
$dateModifiedOn = $item.TimeLastModified
$hashSourceProp = $item.Properties
$strVerComment = $item.CheckInComment
$bolMajorVer = $item.MinorVersion -eq 0 -and $true,$false
$streamFile = $item.OpenBinaryStream()
}
Write-Host "Version is " $fileSourceVer.VersionLabel
Write-Host "Iem Name" $item.Name
Write-Host "USer Created" $userCreatedBy.Name
Write-Host "USer Modified" $userModifiedBy.Name
Write-Host "Date Created" $dateCreatedOn
write-Host "Date Modiifed" $dateModifiedOn
write-Host "Adding now "
$newFile = $folder.Files.Add($folder.Url + "/Destination/" + $item.Name,$streamFile ,$hashSourceProp,$userCreatedBy,$userModifiedBy,$dateCreatedOn,$dateModifiedOn,$strVerComment,$true)
$itmNewVersion = $newFile.Item;
$itmNewVersion["Created"] = $dateCreatedOn
$itmNewVersion["Modified"] = $dateModifiedOn
$itmNewVersion.UpdateOverwriteVersion()
}
}
$web.Update()
$web.Dispose()

You may like following PowerShell tutorials:

This tutorial, we learned how to copy files between libraries using PowerShell in SharePoint 2013/2016.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>