In this post, I am going to share PnP and CSOM based PowerShell scripts to find a file document is already exists or not in the SharePoint Online document library. We can check the file’s availability by its relative URL, absolute URL, Unique ID and Document ID of the file.
Summary
- Check File Exists in SPO Site Library using PnP PowerShell
- Check File Exists in SPO Site Library using CSOM PowerShell
- Check File Exists by Unique Id
- Check File Exists by Document Id
Check File Exists in Document Library using PnP PowerShell
The PnP PowerShell is the easiest way to work with SPO. Before start, you need to replace the following input parameters in the below script and run the commands to check the file exists in the given file path.
- $SiteURL – Specify your SharePoint site URL.
- $FileURL – Provide the required file’s site relative URL or absolute URL.
#Provide your Site URL
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
#Provide the required file's site relative URL or absolute URL
$FileURL = "/document_library/test_file.txt"
#$FileURL = "https://contoso.sharepoint.com/sites/site_name/document_library/test_file.txt"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Get File object by URL.
$File = Get-PnPFile -Url $FileURL -ErrorAction SilentlyContinue
#Check if File already exists
If($File) {
Write-host -ForegroundColor Green "File Exists"
} Else {
Write-host -ForegroundColor Yellow "File Doesn't Exist"
}
Check File Exists in Document Library using CSOM PowerShell
Run the below PowerShell commands to check if a file exists or not in a SharePoint site using the CSOM script. To use CSOM, we need to install Microsoft SharePoint Online Client SDK components and load the required assembly files.
#Add required references to SharePoint client assembly to use CSOM
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
#Specify SharePoint or OneDrive site admin account
$adminAccount = "[email protected]"
$adminPwd = "<user_password>"
#Specify SharePoint Online Site URL or User's OneDrive Site URL
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
#$SiteURL = "https://contoso-my.sharepoint.com/personal/username_domainame_com"
#Provide the required file's site relative URL
$FileRelativeURL= "/sites/site_name/document_library/test_file.txt"
#Setup SharePoint site context
$secPwd = $(ConvertTo-SecureString $adminPwd -asplaintext -force)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($adminAccount,$secPwd)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$ctx.Credentials = $credentials
Try
{
#Get File object from URL
$File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeURL)
$Ctx.Load($File)
$Ctx.ExecuteQuery()
Write-host -ForegroundColor Green "File Exists"
}
Catch
{
Write-host -ForegroundColor Yellow "File Doesn't Exist"
Write-Host -Foreground Red $_
}
Check File Exists by Unique Id
We can also get the file details by using the file’s UniqueId property. We can search files by using a CAML query to filter the files by UniqueId column.
#Load required SharePoint client assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
#Specify SharePoint Online Site URL or name of your Document library/List
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$DocumentLibrary ="Documents"
#Provide the required file's unique ID
$fileUniqueId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
#Get user credentials to connect
$cred = Get-Credential
#Setup SharePoint site context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.Password)
#Get file by Unique Id
$list = $ctx.Web.Lists.GetByTitle($DocumentLibrary)
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml ="@<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='UniqueId' /><Value Type='GUID'>$($fileUniqueId)</Value></Eq></Where></Query></View>";
$listItems=$list.GetItems($camlQuery)
$ctx.Load($listItems)
$ctx.ExecuteQuery()
if($listItems.Count -gt 0) {
Write-host -ForegroundColor Green "File Exists"
Write-host $listItems[0].FieldValues['FileLeafRef']
} else {
Write-host -ForegroundColor Yellow "File Doesn't Exist"
}
Check File Exists by Document Id
SharePoint supports the Document ID feature at a site collection level that assigns a unique ID in each document uploaded to SharePoint. The documents also get a permanent URL with this unique Document ID. So URL will not be changed if the document is moved to another location within the same site collection. The Unique ID property is unique to a list/library whereas the Document Id property is unique to the entire site collection.
The Document Id value is stored inside a column whose internal name is _dlc_DocId. The document’s URL is stored in the internal column EncodedAbsUrl. We can make a CAML query on the _dlc_DocId column and then fetch the details.
#Load required SharePoint client assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
#Specify SharePoint Online Site URL and name of your Document library/List
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$DocumentLibrary ="Documents"
#Provide the required file's Document ID
$DocumentId = "UN6HMTSA4Q6V-1362046625-3"
#Get user credentials to connect
$cred = Get-Credential
#Setup SharePoint site context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.Password)
#Get file by Document Id (_dlc_DocId)
$list = $ctx.Web.Lists.GetByTitle($DocumentLibrary)
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml ="@<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='_dlc_DocId' /><Value Type='Text'>$($DocumentId)</Value></Eq></Where></Query></View>";
$listItems=$list.GetItems($camlQuery)
$ctx.Load($listItems)
$ctx.ExecuteQuery()
if($listItems.Count -gt 0) {
Write-host -ForegroundColor Green "File Exists"
Write-host $listItems[0].FieldValues['FileLeafRef']
} else {
Write-host -ForegroundColor Yellow "File Doesn't Exist"
}