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.

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:

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.

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:

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:
- Check if a List Exists in SharePoint Site using PnP PowerShell
- Get All Lists and Libraries from SharePoint Online Site using PnP PowerShell
- Copy Items from One List to Another in SharePoint Online Using PnP PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.