SharePoint Online: Check If a File Exists in Document Library using PowerShell
Requirement: Check if a file exists in the SharePoint Online document library using PowerShell.
PowerShell to check if a file exists in SharePoint Online document library:
You may often want to check if a file exists in a SharePoint Online document library before taking certain actions. For example, you may want to delete the file if it exists (Otherwise, do some possible workarounds if it doesn’t exist). This blog post will show you how to use PowerShell to determine whether a specific file exists in a document library. If you need to check if a file exists in a document library using PowerShell, there are several ways to do it.
Let’s check if a file exists in the SharePoint document library with PowerShell.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Function to Check if file exists in given URL
Function Check-FileExists($SiteURL, $FileRelativeURL, $Credentials)
{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
Try {
#Try to get the File from URL
$File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeURL)
$Ctx.Load($File)
$Ctx.ExecuteQuery()
Return $True
}
Catch {
Return $False
}
}
#Set Variables for Site URL, List Name and Column Name
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$FileRelativeURL="/sites/Sales/TeamDocuments/LegalTemplate.docx"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Call the function to Check Column Exists in given list
$FileExists = Check-FileExists -SiteURL $SiteURL -FileRelativeURL $FileRelativeURL -Credentials $Cred
if($FileExists) {
write-host "File Exists in the Given URL!" -f Green
#Proceed with your script
}
else {
write-host "File Doesn't Exists in the given URL!" -f Red
}
This checks if a file exists in the SharePoint document library with CSOM PowerShell.
PnP PowerShell to Check File Exists in Document Library
Here is how to check file exists in the SharePoint library with the PnP PowerShell cmdlet Get-PnPFile:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/dochub"
$FileSiteRelativeURL = "/documents/2018/IC Papers.txt"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Check if File exists Already
$FileExists = Get-PnPFile -Url $FileSiteRelativeURL -ErrorAction SilentlyContinue
If($FileExists)
{
Write-host -f Green "File Exists!"
}
Else
{
Write-host -f Yellow "File Doesn't Exists!"
}
You can also use the Get-PnPListItem cmdlet to check if a file exists. By using these scripts, you can quickly and easily check if a file exists in a document library and perform the necessary actions.