How to Check if a Folder Exists in SharePoint Online using PnP PowerShell?

Recently, I worked on a PowerShell script to check if a folder exists in SharePoint Online using PnP PowerShell. I thought I would share this in this tutorial.

Let me show you with real examples.

Check if a Folder Exists in SharePoint Online using PnP PowerShell

I have a SharePoint document library in which I have created a folder. The screenshot below shows this.

pnp powershell check if folder exists in SharePoint

Now, we will use various methods to check if the folder exists in the SharePoint document library using PnP PowerShell.

Method 1: Using Get-PnPFolder

In PnP PowerShell, you can use the Get-PnPFolder to check if a folder exists in a SharePoint Online site. If the folder exists, it returns the folder object; otherwise, it returns nothing.

Here is the complete PnP PowerShell script.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive
# Define the folder path
$folderPath = "/sites/PowerShellFAQs/Shared Documents/Accounting"

# Check if the folder exists
$folder = Get-PnPFolder -Url $folderPath -ErrorAction SilentlyContinue

if ($folder) {
    Write-Host "Folder exists."
} else {
    Write-Host "Folder does not exist."
}

Once you execute the above PnP PowerShell script, it will display the message “Folder exists.” as the folder exists in the SharePoint Online document library. You can see the screenshot below:

Check if a Folder Exists in SharePoint Online using PnP PowerShell

Read Check if a SharePoint Site Exists using PnP PowerShell

Method 2: Using Resolve-PnPFolder

The Resolve-PnPFolder cmdlet is another method in PnP PowerShell that not only checks if a folder exists but also creates it if it does not exist.

Here is an example.

$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive
# Define the folder path
$folderPath = "/sites/PowerShellFAQs/Shared Documents/MyFolder"

# Resolve the folder (creates if not exists)
$folder = Resolve-PnPFolder -SiteRelativePath $folderPath

if ($folder) {
    Write-Host "Folder exists or has been created successfully."
} else {
    Write-Host "Failed to resolve or create the folder."
}

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

Check if a Folder Exists in SharePoint Online using powershell

Method 3: Using Get-PnPListItem

Let me show you another method to check if a folder exists in SharePoint Online.

You can use the Get-PnPListItem cmdlet to search for the folder within the SharePoint document library.

Here is an example and the complete PnP PowerShell script.

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

# Define the document library and folder name
$libraryName = "Shared Documents"
$folderName = "Accounting"

# Get the list items in the document library
$listItems = Get-PnPListItem -List $libraryName -Fields "FileLeafRef" -PageSize 1000

# Check if the folder exists
$folderExists = $listItems | Where-Object { $_["FileLeafRef"] -eq $folderName }

if ($folderExists) {
    Write-Host "Folder exists."
} else {
    Write-Host "Folder does not exist."
}

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

powershell pnp sharepoint check if folder exists

Conclusion

I hope you can use any of these three methods to check if a folder exists in a SharePoint Online document library or list. I recommended using the Get-PnPFolder method to check if a folder exists in SharePoint.

If you still have any questions feel free to leave a comment below, I will check and reply.

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.