SharePoint Online: Add New List Item using PowerShell

Requirement: When working with SharePoint Online, I had a requirement to add new list items to a list from the PowerShell script. This blog post will cover how to create a list item in SharePoint Online using PowerShell. Here are some examples:

SharePoint Online: PowerShell to Add List Item

There are many situations where you may want to create a list item in SharePoint Online without going through the web-based user interface in your SharePoint Online. Luckily, we have an easy way to do this using PowerShell. In this blog post, we will show you how to add an item to a SharePoint Online list using PowerShell.

To add a list item using PowerShell, we’ve to set the value for each custom column in the list. Here is the SharePoint Online PowerShell to create a list item:

#Load SharePoint CSOM Assemblies
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"

#Set Config Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName="Tasks"

Try { 
    #Get Credentials to connect
    $Cred= Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Get the List
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
 
    #sharepoint online powershell add list item
    $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation  
    $ListItem = $List.AddItem($ListItemInfo)
    
    #Set Column Values
    $ListItem["Title"] = "Project Darwin"

    #Set People Picker Field value
    $AssignedToUser = $Ctx.web.EnsureUser("[email protected]")
    $ListItem["AssignedTo"] = $AssignedToUser
    
    #Set Date Fields
    $ListItem["StartDate"] = "01/07/2015"
    $ListItem["DueDate"] = "01/08/2015"
    
    #Set Percentage Field 
    $ListItem["PercentComplete"] = "0.2" #20%

    #Apply changes to list
    $ListItem.Update()
    $Ctx.ExecuteQuery()
 
    Write-host -f Green "New Item has been added to the List!"
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}  

This inserts a new item into the Tasks list. Here is the result: Add item to list in SharePoint Online using PowerShell.

sharepoint online powershell add item to list

SharePoint Online PowerShell to Create List Item

Let’s add some error handling code and get the credentials at run time in the script. Here is the SharePoint Online PowerShell CSOM to add list items: This script randomly creates list items in bulk.

#Load SharePoint CSOM Assemblies
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"

#Config Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Random"
$ItemsToCreate="1000"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
        
    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    #add an item to list SharePoint Online PowerShell - with Random Data
    For($i=1; $i -le $ItemsToCreate; $i++)
    {
        #create list item in sharepoint online using powershell
        $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
        $ListItem = $List.AddItem($ListItemInfo)
        $ListItem["Title"] = $I
        $ListItem.Update()
        $Ctx.ExecuteQuery()

        Write-host "New Item $I Added to the List!" -ForegroundColor Green  
    }
}
Catch {
    write-host -f Red "Error Adding Items to List!" $_.Exception.Message
}

PnP PowerShell to Add List Item in SharePoint Online

You can use these scripts to help automate creating content that needs to be created on a regular basis or unattended. Here is how to add an item to the list in SharePoint Online with the PnP PowerShell cmdlet Add-PnPListItem

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Add List Item - Internal Names of the columns: Value
Add-PnPListItem -List $ListName -Values @{"Title" = "SharePoint 2016 Migration V2"; "ProjectID"="Abj-IT-3025"}

How about Setting Fields like Managed metadata, Hyperlink, Lookup, Person or Group, etc.?
The above scripts work just fine with simple field types such as single lines of text, choice, Yes/No, etc. However, we have to handle them differently for other field types such as Managed metadata, Hyperlink, Lookup, People Picker, etc. Here are my posts to help:

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply

Your email address will not be published.