Recently, I got a requirement to check whether a specific file exists in a SharePoint document library. PnP PowerShell is one of the best choices for this kind of requirement. In this tutorial, I will show you how to check if a file exists in a SharePoint document library using PnP PowerShell with examples.
Check if a File Exists in a SharePoint Document Library Using PnP PowerShell
I will show you here two methods to check if a file exists in a document library in SharePoint Online using PnP PowerShell.
- Using
Get-PnPFile - Using
Get-PnPFolderItem
Method 1: Using Get-PnPFile
The Get-PnPFile cmdlet is the best way to check if a file exists in a SharePoint document library. Here’s how you can use it:
Let’s say we want to check if a file named Report.xlsx exists in the Documents library of our SharePoint site. Then, you can write the complete PnP PowerShell script.
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerShellFAQs" -Interactive
$fileUrl = "/sites/PowerShellFAQs/Shared Documents/Report.xlsx"
# Try to get the file
$file = Get-PnPFile -Url $fileUrl -ErrorAction SilentlyContinue
if ($file -ne $null) {
Write-Host "File exists."
} else {
Write-Host "File does not exist."
}In this script, we use Get-PnPFile to attempt to retrieve the file. If the file exists, it will be retrieved, and we print “File exists.” If the file does not exist, an exception is thrown, and we catch it to print “File does not exist.”
I executed the above PowerShell script, and you can see the output in the screenshot below:

Method 2: Using Get-PnPFolderItem
Another method to check for a file’s existence in a SharePoint library is by using the Get-PnPFolderItem cmdlet, which can list all items in a folder and then check if the specific file is among them.
Here is the complete PowerShell script.
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerShellFAQs" -Interactive
$folderUrl = "Shared Documents"
$fileName = "Report.xlsx"
$items = Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl
$fileExists = $items | Where-Object { $_.Name -eq $fileName }
if ($fileExists) {
Write-Host "File exists."
} else {
Write-Host "File does not exist."
}I executed the above PnP PowerShell script, and it shows that the file exists as the file is available in the SharePoint document library. You can see the output in the screenshot below:

Conclusion
In this tutorial, I have explained two different methods for checking if a file exists in a SharePoint Online document library using PnP PowerShell. I am sure this will help you. Please let me know in the comment section below if you face any issues.
You may also like the following tutorials:
- Check if a List Exists in SharePoint Site using PnP PowerShell
- Check if a Folder Exists in SharePoint Online using PnP PowerShell
- Get All Lists and Libraries from SharePoint Online Site using PnP PowerShell
- Check If a Field Exists in a SharePoint Online List 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.