Insert/Add item to SharePoint Online list using PowerShell

This PowerShell SharePoint tutorial explains, how we can insert an item to SharePoint Online list using PowerShell. We can easily add items to a SharePoint Online list using PowerShell.

In this particular SharePoint PowerShell example, I have a SharePoint Online list name as “Logging” which has below two columns:

  • Title
  • Description

By using CSOM PowerShell we will see how can add an item to the SharePoint Online list.

To work with PowerShell SharePoint Online, you need to make sure, you have the required below client dlls.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

SharePoint Online PowerShell add list item

Below is the PowerShell script to add an item to SharePoint Online list which you can write in Windows PowerShell ISE or also you can use Visual Studio Code to write and debug PowerShell Script.

Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
$SiteURL = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$ListName="Logging"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("bhawana@xxxxxxxxx.onmicrosoft.com", $securePassword)
$Web = $Context.Web
$List = $web.get_lists().getByTitle($ListName)
$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$listItem = $list.addItem($itemCreateInfo)
$listItem.set_item('Title', 'Item Title')
$listItem.set_item('Description', 'Item Description')
$listItem.update();
$Context.Load($listItem)
$Context.ExecuteQuery()

Once you run the code, you can see the items will be added successfully to the SharePoint online list.

PowerShell Insert items to SharePoint Online list
PowerShell Insert items to SharePoint Online list

PowerShell insert multiple or bulk items to SharePoint Online list

The above code will add a single item to the SharePoint Online list. But you can also add multiple items to SharePoint Online list using PowerShell CSOM.

Below is the PowerShell script.

Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
$SiteURL = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$ListName="Logging"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString "*******" -AsPlainText -Force
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("******@onlysharepoint2013.onmicrosoft.com", $securePassword)
$Web = $Context.Web
$List = $web.get_lists().getByTitle($ListName)
for ($i=1; $i -le 10; $i++)
{
$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$listItem = $list.addItem($itemCreateInfo)
$listItem["Title"] = "Item-$($i)"
$listItem["Description"] = "Description-$($i)"
$listItem.Update()
$Context.ExecuteQuery()
}

The above PowerShell script will add 10 items to the SharePoint Online list.

PowerShell add multiple or bulk items to SharePoint Online list
PowerShell add multiple or bulk items to SharePoint Online list

Add item to SharePoint online list PnP PowerShell

We can also use PnP PowerShell to add item to SharePoint Online list.

Below is the PnP PowerShell script to add an item to the SharePoint Online list.

$SiteURL = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$ListName ="Logging"
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
Add-PnPListItem -List $ListName -Values @{"Title" = "Item 1"; "Description"="This is the description to add item to SharePoint Online list using PowerShell"}

Read some SharePoint PowerShell tutorials:

Tags: powershell add item to list, powershell add item to list sharepoint, sharepoint online powershell add list item, add item to sharepoint list using powershell, add new item to sharepoint list using powershell, sharepoint online powershell add list item, csom powershell add list item, powershell add to sharepoint online list, Insert item to SharePoint Online list using PowerShell

I hope this article will be helpful to insert items to list using PowerShell in SharePoint Online.

  • >