How to Create a List in SharePoint Online using PowerShell or PnP PowerShell?

In SharePoint Online, we create lists and libraries to store information and documents. It is easy to create a list in a SharePoint site using the browser, but it is a little different when you create a SharePoint list using PowerShell.

In this tutorial, I will explain how to create a list in SharePoint Online using PowerShell and PnP PowerShell.

Create a List in SharePoint Online using PowerShell

In the first example, I will show you how to create a list in SharePoint Online using the SharePoint management shell.

Below is the complete PowerShell script for creating a custom list named “Project Tasks” on the SharePoint site.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerPlatformFAQs"
# Define Variables
$listTitle = "Project Tasks"
$listDescription = "A list to track project tasks."

$UserName = "bijay@szg52.onmicrosoft.com"
$Password = "Enter Your Password"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
   
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$Ctx.Credentials = $Cred

$web = $ctx.Web

# Create the list
$listCreationInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$listCreationInfo.Title = $listTitle
$listCreationInfo.TemplateType = [Microsoft.SharePoint.Client.ListTemplateType]::GenericList
$list = $web.Lists.Add($listCreationInfo)
$list.Description = $listDescription
$list.Update()
$ctx.ExecuteQuery()

I executed the above script successfully; you can see below:

Create a List in SharePoint Online using PowerShell

Once the script is executed successfully, when you open the SharePoint Online site, you can see the list was created successfully.

powershell create list sharepoint

Check out Create, Read, Update, and Delete an Item from a SharePoint Online List using PnP PowerShell

Add Columns to SharePoint List using PowerShell

Using PowerShell, let me show you how to add columns to a SharePoint list.

Single Line of Text Column

Below is the code to a single line of text column in a SharePoint list.

# Add a single line of text column
$fieldXml = "<Field DisplayName='Task Name' InternalName='TaskName' Type='Text' />"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

If you want to have additional settings you can change the script like below:

# Add a single line of text column with additional settings
$fieldXml = "<Field DisplayName='Task Description' InternalName='TaskDescription' Type='Text' Required='TRUE' />"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

Choice Column

Below is the code to create a choice column in a SharePoint Online list.

# Add a choice column
$fieldXml = "<Field DisplayName='Priority' InternalName='Priority' Type='Choice' Format='Dropdown'>
                <CHOICES>
                    <CHOICE>High</CHOICE>
                    <CHOICE>Medium</CHOICE>
                    <CHOICE>Low</CHOICE>
                </CHOICES>
                <Default>Medium</Default>
             </Field>"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

For additional settings in a choice column, you can write the script like below:

# Add a choice column with additional settings
$fieldXml = "<Field DisplayName='Status' InternalName='Status' Type='Choice' Format='Dropdown' Required='TRUE'>
                <CHOICES>
                    <CHOICE>Not Started</CHOICE>
                    <CHOICE>In Progress</CHOICE>
                    <CHOICE>Completed</CHOICE>
                </CHOICES>
                <Default>Not Started</Default>
             </Field>"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

Check out Add a Choice Column to a SharePoint Online List Using PnP PowerShell

Yes/No Column

Here is the script to add a Yes/No column into a SharePoint Online list.

# Add a yes/no column
$fieldXml = "<Field DisplayName='Completed' InternalName='Completed' Type='Boolean' />"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

Person or Group Column

Here is the PowerShell script to create a person or group column in a SharePoint Online list.

# Add a person or group column
$fieldXml = "<Field DisplayName='Assigned To' InternalName='AssignedTo' Type='User' UserSelectionMode='PeopleAndGroups' />"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

Below is the complete PowerShell script that will create the SharePoint Online list and add additional columns.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerPlatformFAQs"
# Define Variables
$listTitle = "Project Tasks"
$listDescription = "A list to track project tasks."

$UserName = "bijay@szg52.onmicrosoft.com"
$Password = "Enter Your Password"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force

# Create a new list
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$web = $ctx.Web

$listCreationInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$listCreationInfo.Title = $listTitle
$listCreationInfo.TemplateType = [Microsoft.SharePoint.Client.ListTemplateType]::GenericList
$list = $web.Lists.Add($listCreationInfo)
$list.Description = $listDescription
$list.Update()
$ctx.ExecuteQuery()

# Add a single line of text column
$fieldXml = "<Field DisplayName='Task Name' InternalName='TaskName' Type='Text' />"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

# Add a choice column
$fieldXml = "<Field DisplayName='Priority' InternalName='Priority' Type='Choice' Format='Dropdown'>
                <CHOICES>
                    <CHOICE>High</CHOICE>
                    <CHOICE>Medium</CHOICE>
                    <CHOICE>Low</CHOICE>
                </CHOICES>
                <Default>Medium</Default>
             </Field>"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

# Add a yes/no column
$fieldXml = "<Field DisplayName='Completed' InternalName='Completed' Type='Boolean' />"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

# Add a person or group column
$fieldXml = "<Field DisplayName='Assigned To' InternalName='AssignedTo' Type='User' UserSelectionMode='PeopleAndGroups' />"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

# Add a single line of text column with additional settings
$fieldXml = "<Field DisplayName='Task Description' InternalName='TaskDescription' Type='Text' Required='TRUE' />"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

# Add a choice column with additional settings
$fieldXml = "<Field DisplayName='Status' InternalName='Status' Type='Choice' Format='Dropdown' Required='TRUE'>
                <CHOICES>
                    <CHOICE>Not Started</CHOICE>
                    <CHOICE>In Progress</CHOICE>
                    <CHOICE>Completed</CHOICE>
                </CHOICES>
                <Default>Not Started</Default>
             </Field>"
$list.Fields.AddFieldAsXml($fieldXml, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$ctx.ExecuteQuery()

# Disconnect from SharePoint Online
Disconnect-SPOService

This is how to create a list in SharePoint Online and add additional columns to it.

Read Delete and Recover a SharePoint Online Site Using PowerShell and PnP PowerShell

Create a SharePoint Online List using PnP PowerShell

Now, let me show you how to create a SharePoint Online list using PnP PowerShell.

In PnP PowerShell, you can use the New-PnPList cmdlet to create a list on the SharePoint Online site.

Here is the complete PowerShell script:

# Create a new list
$siteUrl = "https://szg52.sharepoint.com/sites/MarketingTeam"
Connect-PnPOnline -Url $siteUrl -Credentials (Get-Credential)

New-PnPList -Title "Marketing Tasks" -Template GenericList

Once you execute the above PowerShell script, you can see the output in the screenshot below:

Create a SharePoint Online List using PnP PowerShell

Now, if you open your SharePoint site, you can see the list created successfully using PnP PowerShell.

Create a SharePoint List using PnP PowerShell

This is how to create a custom list in the SharePoint Online site using PnP PowerShell.

If you want to add columns to the SharePoint list, you can use the below script. I have explained how to add a single line text column, choice column, yes/no column, person or group column, etc.

# Add a single line of text column
Add-PnPField -List "Project Tasks" -DisplayName "Task Name" -InternalName "TaskName" -Type Text

# Add a choice column
Add-PnPField -List "Project Tasks" -DisplayName "Priority" -InternalName "Priority" -Type Choice -AddToDefaultView `
-Choices "High", "Medium", "Low" -DefaultValue "Medium"

# Add a yes/no column
Add-PnPField -List "Project Tasks" -DisplayName "Completed" -InternalName "Completed" -Type Boolean -AddToDefaultView

# Add a person or group column
Add-PnPField -List "Project Tasks" -DisplayName "Assigned To" -InternalName "AssignedTo" -Type User -AddToDefaultView

If you want to have some additional settings, you can write like below, here I have added a few properties like -Required, -DefaultValue, etc.

# Add a single line of text column with additional settings
Add-PnPField -List "Project Tasks" -DisplayName "Task Description" -InternalName "TaskDescription" -Type Text `
-Required $true -AddToDefaultView

# Add a choice column with additional settings
Add-PnPField -List "Project Tasks" -DisplayName "Status" -InternalName "Status" -Type Choice -AddToDefaultView `
-Choices "Not Started", "In Progress", "Completed" -DefaultValue "Not Started" -Required $true

Here is the complete PowerShell script:

# Define Variables
$siteUrl = "https://szg52.sharepoint.com/sites/MarketingTeam"
$listTitle = "Project Tasks"

# Connect to SharePoint Online
Connect-PnPOnline -Url $siteUrl -Credentials (Get-Credential)

# Create a new list
New-PnPList -Title $listTitle -Template GenericList

# Add a single line of text column
Add-PnPField -List $listTitle -DisplayName "Task Name" -InternalName "TaskName" -Type Text -AddToDefaultView

# Add a choice column
Add-PnPField -List $listTitle -DisplayName "Priority" -InternalName "Priority" -Type Choice -AddToDefaultView `
-Choices "High", "Medium", "Low" -DefaultValue "Medium"

# Add a yes/no column
Add-PnPField -List $listTitle -DisplayName "Completed" -InternalName "Completed" -Type Boolean -AddToDefaultView

# Add a person or group column
Add-PnPField -List $listTitle -DisplayName "Assigned To" -InternalName "AssignedTo" -Type User -AddToDefaultView

# Add a single line of text column with additional settings
Add-PnPField -List $listTitle -DisplayName "Task Description" -InternalName "TaskDescription" -Type Text `
-Required $true -AddToDefaultView

# Add a choice column with additional settings
Add-PnPField -List $listTitle -DisplayName "Status" -InternalName "Status" -Type Choice -AddToDefaultView `
-Choices "Not Started", "In Progress", "Completed" -DefaultValue "Not Started" -Required $true

# Disconnect from SharePoint Online
Disconnect-PnPOnline

Conclusion

When you want to create lists in a SharePoint production environment, you mostly use PowerShell or PnP PowerShell. In this tutorial, I have explained how to create a list in SharePoint Online using PowerShell and PnP PowerShell. I have also explained how to add columns to a SharePoint Online list using PnP PowerShell and PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.