How to Check if a List Exists in SharePoint Site using PnP PowerShell?

Recently, I was working on a SharePoint project where we wanted to write a script to create a SharePoint list. However, it first needs to check whether the list already exists. In this tutorial, I will show you how to check if a list exists on the SharePoint site using PnP PowerShell.

There are different ways to do it. I will show here three different methods to check if a list exists in SharePoint using PnP PowerShell.

I will also show you how to check if a list item exists in SharePoint Online using PnP PowerShell.

Check if a List Exists in SharePoint Site using PnP PowerShell

Here are the different methods to check if a list exists on a SharePoint Online site.

1. Using Get-PnPList

The best way to check if a list exists in SharePoint Online is by using the Get-PnPList cmdlet. This cmdlet retrieves a list based on its title, ID, or URL. If the list exists, it will return the list object; otherwise, it will throw an error.

Here is a script to check if a list exists by its title in a SharePoint Online site. It will work for both SharePoint lists and document libraries.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive
# Provide your list title
$listTitle = "Employee onboarding"

try {
    $list = Get-PnPList -Identity $listTitle
    Write-Host "List '$listTitle' exists."
} catch {
    Write-Host "List '$listTitle' does not exist."
}

I executed the above PowerShell script using VS code, and you can display the message as the list exists, as the list exists in the SharePoint Online site. You can see the screenshot below:

Check if a List Exists in SharePoint Site using PnP PowerShell

2. Using Try-Catch Block

Let me show you another method to check if a SharePoint Online list exists using PnP PowerShell. You can use a Try-Catch block to handle the exception if the list does not exist.

Here’s a script using the Try-Catch block to check if a SharePoint list exists using PnP PowerShell.

# Replace with your list title
$listTitle = "Documents"

try {
    $list = Get-PnPList -Identity $listTitle
    if ($list) {
        Write-Host "List '$listTitle' exists."
    }
} catch {
    Write-Host "List '$listTitle' does not exist."
}

I executed the above PowerShell script, and you can see the output in the screenshot below:

PowerShell check if a SharePoint Online lists exists

3. Using Client-Side Object Model (CSOM)

You can also use CSOM with PnP PowerShell to check if a list exists in SharePoint Online.

Here is a script using CSOM:

# Replace with your list title
$listTitle = "Documents"

# Load the client context
$context = Get-PnPContext

# Get the list by title
$list = $context.Web.Lists.GetByTitle($listTitle)
$context.Load($list)

try {
    $context.ExecuteQuery()
    Write-Host "List '$listTitle' exists."
} catch {
    Write-Host "List '$listTitle' does not exist."
}

Check out Get All List Fields in SharePoint Online using PnP PowerShell

Check if SharePoint List Item Exists using PnP PowerShell

I will show you how to check if a list item exists in SharePoint Online using PnP PowerShell. We will discuss different methods.

Here, I have created a list on the SharePoint Online site and added a few items to the list. The list has only the Title column. Overall, the SharePoint list looks like below:

Check if SharePoint List Item Exists

We will use this SharePoint list to check if the item exists in the list.

Method 1: Using Get-PnPListItem

In the first method, I will show you how to use the Get-PnPListItem PnP PowerShell cmdlet to get SharePoint list items and then check if a specific item exists based on a condition.

Here is the complete script, where I have used the Title column to check if the SharePoint list item exists.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive

$listTitle = "Departments"
$itemTitle = "Marketing"

$listItems = Get-PnPListItem -List $listTitle -PageSize 1000

$itemExists = $false
foreach ($item in $listItems) {
    if ($item["Title"] -eq $itemTitle) {
        $itemExists = $true
        break
    }
}

if ($itemExists) {
    Write-Host "Item exists in the list."
} else {
    Write-Host "Item does not exist in the list."
}

Since the list item already exists, you can see it is showing the “Item exists in the list.”. Look at the screenshot below, I executed the above PnP PowerShell script using VS code.

PnP PowerShell Check if SharePoint List Item Exists

Read Check if a File Exists in a SharePoint Document Library Using PnP PowerShell

Method 2: Using CAML Query

Let me show you another method to check if a list item exists in SharePoint Online using PnP PowerShell. We can also use CAML.

CAML (Collaborative Application Markup Language) queries can be used to filter items directly on the server side, which can be more efficient.

Here is an example and I am checking the same item in the above SharePoint Online list.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive

$listTitle = "Departments"
$itemTitle = "Marketing"

$camlQuery = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$itemTitle</Value></Eq></Where></Query></View>"
$listItems = Get-PnPListItem -List $listTitle -Query $camlQuery

if ($listItems.Count -gt 0) {
    Write-Host "Item exists in the list."
} else {
    Write-Host "Item does not exist in the list."
}

I executed the above PnP PowerShell script using VS code, and you can see the output in the screenshot below.

Check if SharePoint List Item Exists using PnP PowerShell

Method 3: Using Get-PnPListItem with Filter

Let me show you another efficient method to check if a SharePoint list item exists. We can use the -Filter parameter with Get-PnPListItem to directly filter items from a SharePoint Online list.

Here is an example.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive

$listTitle = "Departments"
$itemTitle = "Marketing"

$listItems = Get-PnPListItem -List $listTitle -Filter "Title eq '$itemTitle'"

if ($listItems.Count -gt 0) {
Write-Host "Item exists in the list."
} else {
Write-Host "Item does not exist in the list."
}

You can use any of the three methods to check if a list item exists in a SharePoint Online list using PnP PowerShell.

Conclusion

In this tutorial, I have explained how to check if a list exists in a SharePoint site using PnP PowerShell, using different methods and examples. I recommend using the Get-PnPList cmdlet to check if the SharePoint Online list exists using PnP PowerShell.

Do let me know if you still have any questions.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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