Top Menu

Friday, March 29, 2013

PowerShell - List Data Migration

Scenario:
I had to migrate data from ListA to ListB with the condition that values in Created By, Create Date, Updated By and Update Date columns must not be changed in ListB.

So I wrote this little PowerShell script to do the job.

Solution:
cls;

[void][reflection.assembly]::Loadwithpartialname("Microsoft.SharePoint")

$Site=[Microsoft.SharePoint.SPSite]("http://sharepoint01:1100")
$SiteWeb = $Site.openWeb()
$SourceList = $SiteWeb.Lists["ListA"]
$DestinationList = $SiteWeb.Lists["ListB"]

$ItemsColl = $SourceList.items

write-host "Total items found: " $SourceList.items.count

foreach ($Item in $ItemsColl) 
{

    write-host "Copying Item: " $Item["Title"]

    $NewItem = $DestinationList.items.Add()
    $NewItem["Title"] = $Item["Title"]
    $NewItem["Author"] = $Item["Author"]
    $NewItem["Editor"] = $Item["Editor"]
    $NewItem["Modified"] = $Item["Modified"]
    $NewItem["Created"] = $Item["Created"]
    $NewItem.Update()

}

$Web.dispose
$Site.dispose

write-host "All items have been copied successfully."

No comments:

Post a Comment

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...