Powershell Script to copy the Files from one Folder to another Folder in Site collection with Exact Fields and File Versions

InfoPath alternatives for form designing SharePoint

In this SharePoint 2013 PowerShell tutorial, we will discuss how we can use PowerShell Script to copy the Files from one Folder to another Folder in a Site collection with Exact Fields and File Versions in SharePoint 2013.

Below is the PowerShell script:

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()


Read some SharePoint PowerShell tutorials:

Hope this article will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About Sravan kumar

I am a SharePoint Developer Working in Reputed MNC company Bangalore.

View all posts by Sravan kumar →

Leave a Reply