Our use of cookies

We use cookies to tailor your experience, gather analytics, and provide you with live assitance. By clicking "Accept" or continuing to browse our site you agree to the use of cookies. For more details please read our Cookie Policy.

Script: Create list items in a SharePoint list using PowerShell

Disclaimer: this script is given as is, we do not support it nor take responsibility for any use you make of it.


The script below creates list items in a SharePoint list that contains 100 columns named "Myfield1", "Myfield2", ..., "Myfield100".

To execute it, open a PowerShell prompt, register the script and run it as show below.

. .\CreateListItems.ps1


CreateListItems -spVersion SPOnline -siteUrl "https://yoursite.sharepoint.com/sites/TestHierarchy" -username "username" -password "password" -domain "yourdomain.onmicrosoft.com" -listName "Your list name" -noOfItems 1000 

enum SPVersion { SP2010 SP2013 SP2016 SP2019 SPOnline } Function LoadAssemblies { Param( [Parameter(Mandatory=$true)] [SPVersion]$spVersion ) $SP2010ClientDllPath = "C:\Program Files\Common Files\microsoft shared\SharePoint Client" $SP2013ClientDllPath = "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI" $SP2016ClientDllPath = "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI" $SP2019ClientDllPath = "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI" $SPOnlineClientDllPath = "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI"       $clientDllPath = "Microsoft.SharePoint.Client.dll" $clientRuntimeDllPath = "Microsoft.SharePoint.Client.Runtime.dll" switch ($spVersion) { SP2010 { $clientDllPath = $SP2010ClientDllPath + "\" + $clientDllPath $clientRuntimeDllPath = $SP2010ClientDllPath + "\" + $clientRuntimeDllPath break; } SP2013 { $clientDllPath = $SP2013ClientDllPath + "\" + $clientDllPath $clientRuntimeDllPath = $SP2013ClientDllPath + "\" + $clientRuntimeDllPath break; } SP2016 { $clientDllPath = $SP2016ClientDllPath + "\" + $clientDllPath $clientRuntimeDllPath = $SP2016ClientDllPath + "\" + $clientRuntimeDllPath break; } SP2019 { $clientDllPath = $SP2019ClientDllPath + "\" + $clientDllPath $clientRuntimeDllPath = $SP2019ClientDllPath + "\" + $clientRuntimeDllPath break; } SPOnline { $clientDllPath = $SPOnlineClientDllPath + "\" + $clientDllPath $clientRuntimeDllPath = $SPOnlineClientDllPath + "\" + $clientRuntimeDllPath break; } } $client = [System.Reflection.Assembly]::LoadFile($clientDllPath) $clientRuntime = [System.Reflection.Assembly]::LoadFile($clientRuntimeDllPath) if( !$client -or !$clientRuntime ){ throw "Could not load Microsoft.SharePoint.Client.dll or Microsoft.SharePoint.Client.Runtime.dll" } } Function CreateItems { Param( [Microsoft.SharePoint.Client.Web][Parameter(Mandatory=$true)]$parentWeb, [string][Parameter(Mandatory=$true)]$listName, [int][ValidateRange(1, 1000000)][Parameter(Mandatory=$true)]$noOfItems, [int][ValidateRange(1, 1000000)][Parameter(Mandatory=$true)]$startIndex ) Write-Host "Number of items to create on list $($listName): $noOfItems" $list = $parentWeb.Lists.GetByTitle($listName) for ($l = 1; $l -le $noOfItems; $l++) { $listItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation $newItem = $list.AddItem($listItemInfo) $newItem["Title"] = "This is item no. $($l + $startIndex - 1)" for ($j = 1; $j -le 100; $j++) { # fill your fields here $fieldName = "MyField$($j)"; $newItem[$fieldName] = $j } $newItem.Update() if(0 -eq $l % 10) # adjust this accordingly to your item size to avoid teh error "The request message is too big. The server does not allow messages larger than 2097152 bytes." { $parentWeb.Context.ExecuteQuery() Write-Host "$($l) items created." } } # commit leftovers $parentWeb.Context.ExecuteQuery() Write-Host "$($l -1) items created." } Function CreateListItems { Param ( [SPVersion][Parameter(Mandatory=$true)]$spVersion, [string][Parameter(Mandatory=$true)]$siteUrl, [string][Parameter(Mandatory=$true)]$username, [string][Parameter(Mandatory=$true)]$password, [string][Parameter(Mandatory=$true)]$domain, [string][Parameter(Mandatory=$true)]$listName, [int][ValidateRange(1, 1000000)][Parameter(Mandatory=$true)]$noOfItems, [int][ValidateRange(1, 1000000)]$startIndex = 1 ) Write-Host "Running..." LoadAssemblies $spVersion $creds = $null; $secPass = ConvertTo-SecureString $password -AsPlainText -Force $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) if ($spVersion -ne [SPVersion]::SPOnline) { $creds = New-Object System.Net.NetworkCredential($userName,$secPass,$domain) $ctx.AuthenticationMode = [Microsoft.SharePoint.Client.ClientAuthenticationMode]::Default } else { $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("$($username)@$($domain)", $secPass) } $ctx.Credentials = $creds $ctx.Load($ctx.Web) try { $ctx.ExecuteQuery() } catch { throw $_.Exception return } Write-Host "Running..." CreateItems -parentWeb $ctx.Web -listName $listName -noOfItems $noOfItems -startIndex $startIndex }