How to Check if a File Exists in a SharePoint Document Library Using PnP PowerShell?

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:

pnp powershell check if file exists in sharepoint online

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:

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

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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