Add-PSSnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue
$sourceWebURL = "http://srcurl"
$sourceListName = "Source"
$destinationWebURL = "http://desurl"
$destinationListName = "Destination"
$spSourceWeb = Get-SPWeb $sourceWebURL
$spDestinationWeb = Get-SPWeb $destinationWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
$spDestinationList = $spDestinationWeb.Lists[$destinationListName]
$RootFolder = $spDestinationList.RootFolder
$spSourceItems = $spSourceList.Items
ForEach ($item in $spSourceItems)
{
Try
{
$binary = $item.File.OpenBinary();
if ($binary -ne $null)
{
$sBytes = $item.File.OpenBinary()
[Microsoft.SharePoint.SPFile]$spFile = $RootFolder.Files.Add($item.Name, $sBytes, $true)
$theItem = $spFile.Item
write-host -f Green "...Success!"
$pos = $item["Author"].IndexOf("#")
$userAuthorLogin = "domainname\"+$item["Author"].Substring($pos+1)
$pos1 = $item["Editor"].IndexOf("#")
$userEditorLogin = "domainname\"+$item["Editor"].Substring($pos+1)
$dateCreatedToStore = Get-Date $item["Created"]
$dateModifiedToStore = Get-Date $item["Modified"]
$userAuthor = Get-SPUser -Web $spDestinationWeb | ? {$_.userlogin -eq $userAuthorLogin}
$userAuthorString = "{0};#{1}" -f $userAuthor.ID, $userAuthor.UserLogin.Tostring()
$userEditor = Get-SPUser -Web $spDestinationWeb | ? {$_.userlogin -eq $userEditorLogin}
$userEditorString = "{0};#{1}" -f $userEditor.ID, $userEditor.UserLogin.Tostring()
#Sets the created by field
$theItem["Author"] = $userAuthorString
$theItem["Created"] = $dateCreatedToStore
#Set the modified by values
$theItem["Editor"] = $userEditorString
$theItem["Modified"] = $dateModifiedToStore
#Store changes without overwriting the existing Modified details.
$theItem.UpdateOverwriteVersion()
write-host -f Green "...Success!"
}
}
Catch [system.exception]
{
write-host "Caught a system exception for " $item.ID $item.Title
}
Finally
{
$spSourceWeb.Dispose();
$spDestinationWeb.Dispose();
}
}