I have been given a task to make a script in CSOM. I am very new to Powershell so any help would be appreciated.

I have a site collection with multiple calendars within the sites. I have to create a script that will look for new events in multiple calendars and copy the events to a single target calendar. I can copy from a source calendar to a target calendar but cannot seem to get multiple calendars sources.

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Copy-ListItems()
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $SourceListName,
        [Parameter(Mandatory=$true)] [string] $TargetListName
    Try {
        #Setup Credentials to connect
        $Cred = Get-Credential
        $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Get the Source List and Target Lists
        $SourceList = $Ctx.Web.Lists.GetByTitle($SourceListName)
        $TargetList = $Ctx.Web.Lists.GetByTitle($TargetListName)

        #Get All Items from Source List
        $SourceListItems = $SourceList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())

        #Get each column value from source list and add them to target
        ForEach($SourceItem in $SourceListItems)
            $NewItem =New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
            $ListItem = $TargetList.AddItem($NewItem)

            #Map each field from source list to target list - INTERNAL NAMES
            $ListItem["Title"] = $SourceItem["Title"]
            $ListItem["Location"] = $SourceItem["Location"]
            $ListItem["EventDate"] = $SourceItem["EventDate"]
            $ListItem["EndDate"] = $SourceItem["EndDate"]
            $ListItem["Description"] = $SourceItem["Description"]
            $ListItem["Category"] = $SourceItem["Category"]
            $ListItem["RecurrenceData"] = $SourceItem["RecurrenceData"]
            $ListItem["fRecurrence"] = $SourceItem["fRecurrence"]
            $ListItem["TimeZone"] = $SourceItem["TimeZone"]
            $ListItem["XMLTZone"] = $SourceItem["XMLTZone"]
            $ListItem["UID"] = $SourceItem["UID"]
            $ListItem["Duration"] = $SourceItem["Duration"]
            $ListItem["EventType"] = $SourceItem["EventType"]

        write-host  -f Green "Total List Items Copied from '$SourceListName' to '$TargetListName' : $($SourceListItems.count)"
    Catch {
        write-host -f Red "Error Copying List Items!" $_.Exception.Message

#Set Parameters
$SiteURL= "https://site.sharepoint.com/teams/site/"
$SourceListName="Test Calendar"
$TargetListName="Destination Calendar"

#Call the function to copy list items
Copy-ListItems -siteURL $SiteURL -SourceListName $SourceListName -TargetListName $TargetListName

Credit to: https://www.sharepointdiary.com/2017/01/sharepoint-online-copy-list-items-to-another-list-using-powershell.html

I have to copy from multiple sources and skip if duplicate.

Thank you in advance.

1 Answer 1


Add the list titles to an array, and loop through the array.

#Set Parameters
$SiteURL= "https://site.sharepoint.com/teams/site/"
$SourceListNames = @("Test Calendar", "Test Calendar 2", "Test Calendar 3")
$TargetListName="Destination Calendar"

#Call the function to copy list items
foreach($SourceListName in $SourceListNames) {
    Copy-ListItems -siteURL $SiteURL -SourceListName $SourceListName -TargetListName $TargetListName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.