How to check if File exists or not in a SharePoint document library using PowerShell

In this particular SharePoint online tutorial, we will discuss: If the file exists or not inside a document library in SharePoint online using PowerShell? Also, we will check if a particular file exists inside a folder in the SharePoint Online document library using PowerShell.

[toc]

The same way we can check if a file exists in a document library using PowerShell in SharePoint 2013/2016.

All the PowerShell scripts you can run, debug and test in visual studio code or by using Windows PowerShell ISE.

Check if file exists in a document library in SharePoint online using PowerShell

Now, we will check if a file exists in a SharePoint document library using PowerShell.

Below PowerShell script, which will take the relative URL of the file and will check if the file exists on the SharePoint document library or not.

sharepoint powershell check if file exists in library
sharepoint powershell check if file exists in library
$SPOnlineUserName="bijay@******.onmicrosoft.com"
Try{
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
function IsFileExists()
{
$context = New-Object Microsoft.SharePoint.Client.ClientContext("https://******.sharepoint.com/sites/bhawana/")
$securePassword=ConvertTo-SecureString "*******" -AsPlainText -Force
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($SPOnlineUserName, $securePassword)
Try {
$File = $context.web.GetFileByServerRelativeUrl("/sites/bhawana/BijayDocuments/Blogs.txt")
$context.Load($File)
$context.ExecuteQuery()
return $True
}
Catch {
Write-Host $_.Exception.Message
return $False
}
}
$FileExists = IsFileExists
if($FileExists) {
write-host "File Exists"
}
else {
write-host "File Doesn't Exists"
}

The above PowerShell script will return true if the file exists in the document library and false if the file is not exists in the document library.

Check if file exists inside folder in document library in SharePoint online using PowerShell

The below PowerShell script will check if the file exists in any particular folder in SharePoint online document library.

$SiteURL = "https://******.sharepoint.com/sites/bhawana/"
$SPOnlineUserName="bijay@******.onmicrosoft.com"
Try{
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
function IsFileExists()
{
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString "******" -AsPlainText -Force
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($SPOnlineUserName, $securePassword)
Try {
$File = $context.web.GetFileByServerRelativeUrl("/sites/bhawana/BijayDocuments/01-02-18/Useful.txt")
$context.Load($File)
$context.ExecuteQuery()
return $True
}
Catch {
Write-Host $_.Exception.Message
return $False
}
}
$FileExists = IsFileExists
if($FileExists) {
write-host "File Exists"
}
else {
write-host "File Doesn't Exists"
}

The above PowerShell script will check if a file exists inside a folder in the SharePoint Online document library.

How to check the document already exist in the Document library in SharePoint (SPFolder)

Now, we will see how to check the document already exists in the document library in SharePoint using SPFolder class in SharePoint 2016/2013.

The below code will not work in SharePoint Online, because we can not use the server object model code in SharePoint Online.

Here we will use SPFile.Exists property to check if a particular file is exists or not in SharePoint.

using (SPSite site = new SPSite("http://Site URL"))
using (SPWeb web = site.OpenWeb())
{
SPFile file = web.GetFile("/site/TSInfo/HR/hrdocument.docx");
if (file.Exists)
{
//Your code will goes here.
}
}

Check if file exists in SharePoint using CSOM

We can also check if file exists or not in SharePoint using CSOM (.Net client object model)

Below is the code to check if file exists or not in SharePoint using csom.

using(var ctx = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
Microsoft.SharePoint.Client.File file = web.GetFileByServerRelativeUrl("/site/Bhawana/HR/hrdocument.docx");
bool isDocExists = false;
try
{
ctx.Load(file);
ctx.ExecuteQuery();
isDocExists = file.Exists;
}
catch{ }
if (isDocExists )
{
//Your code will goes here.
}
}
private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in "YourPassword")
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

Here, ctx.ExecuteQuery(); will through an error if the document exists in the document library and according to that you need to handle the code in SharePoint.

You may like following SharePoint PowerShell tutorials:

Other tags: file exists or not in document library PowerShell, SharePoint online document library if file exists using PowerShell, file exists or not SharePoint 2013 check, SharePoint 2013 check if file exists, SharePoint PowerShell check if file exists in library, check if folder exists in SharePoint document library PowerShell, check if folder exists in SharePoint using client object model, check if folder exists in SharePoint document library PowerShell, powershell check if file exists in SharePoint.

I hope this article will be helpful to check if a file exists in the document library using PowerShell in SharePoint, SharePoint server object model, and client-side object model.

>