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:
- Unable to Upload Large Files to SharePoint 2013
- Shrink SharePoint config database transaction log files
- Migrate files with a folder structure to SharePoint 2013 document library using PowerShell
- All about _Layouts and _catalogs files in SharePoint 2013
- Microsoft Flow Example: Copy files from one SharePoint Online account or folder to another
- Bulk Export SharePoint Files using PowerShell
- Microsoft added more file extensions to Blocked file types in Outlook on the web
- Download files from SharePoint document library using PowerShell
This tutorial, we learned how to copy files between libraries using PowerShell in SharePoint 2013/2016.
I am a SharePoint Developer Working in Reputed MNC company Bangalore.