SharePoint Online: Check If a Folder Exists using PowerShell
Requirement: Check if a folder exists in the SharePoint Online document library.
SharePoint Online: PowerShell to Check If Folder Exists
When Working with SharePoint Online PowerShell scripts, you may need to determine if a folder exists in SharePoint Online before creating a new folder or deleting an existing folder. In this blog post, we’ll show you how to use PowerShell to quickly check if a folder exists in SharePoint Online!
Here is the PowerShell for SharePoint Online to check if a folder exists:
#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 folder Exists
Function Check-SPOFolderExists()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $FolderRelativeURL
)
Try {
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()
#Check Folder Exists
Try {
$Folder = $Web.GetFolderByServerRelativeUrl($FolderRelativeURL)
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
Write-host -f Green "Folder Exists!"
}
Catch {
Write-host -f Yellow "Folder Doesn't Exist!"
}
}
Catch {
write-host -f Red "Error Checking Folder Exists!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$FolderRelativeURL="/Shared Documents/2018"
#Call the function
Check-SPOFolderExists -SiteURL $SiteURL -FolderRelativeURL $FolderRelativeURL
Alternatively, you can use the other method used in my other article: SharePoint Online: Create a Folder using PowerShell
PnP PowerShell to Check if a folder exists in SharePoint Online
The PnP PowerShell way to check whether a folder exists or not in the given location goes here:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Operations"
$FolderURL = "/sites/Operations/Shared Documents/Classifieds" #Server Relative URL
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Try to Get the Folder
$Folder = Get-PnPFolder -Url $FolderURL -ErrorAction SilentlyContinue
#sharepoint online powershell To check if folder exists
If($Folder -ne $null)
{
Write-Host -f Green "Folder exists!"
}
Else
{
Write-Host -f Yellow "Folder does not exists!"
}
Alright, the above two scripts check whether the given folder exists in the specified URL. How about checking if a folder has a specific sub-folder in SharePoint Online?
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Operations"
$ParentFolderURL = "/Shared Documents" #Site Relative URL
$FolderName = "Classified"
Try{
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Try to Get the Folder from given parent folder
$Folder = Get-PnPFolderItem -FolderSiteRelativeUrl $ParentFolderURL -ItemType Folder -ItemName $FolderName
#Powershell To check if folder exists
If($Folder -ne $null)
{
Write-Host -f Green "Folder exists!"
}
Else
{
Write-Host -f Yellow "Folder does not exists!"
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
How to check if a Document Library has a specific Folder or not using PowerShell?
The script below checks if the given document library has a specific folder (at its root level).
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Operations"
$LibraryName = "Documents"
$FolderName = "Classified"
Try{
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Try to Get the Folder from document library
$Folder = Get-PnPFolder -List $ListName | Select -ExpandProperty Name | Where {$_.Name -eq $FolderName}
# check if folder exists in the document library - ONLY AT ROOT LEVEL
If($Folder -ne $null)
{
Write-Host -f Green "Folder exists!"
}
Else
{
Write-Host -f Yellow "Folder does not exists!"
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
This article shows that we can use PowerShell to determine if a folder exists in SharePoint Online. Using these steps, you can easily verify whether a folder already exists in a document library or list before creating it or verify that a specific folder has been deleted.
What if you want to create a new folder if it doesn’t exist? To create a folder in SharePoint Online using PowerShell, refer: How to create a New Folder in SharePoint Online with PowerShell?
is there a way to use this script to loop through all the list of site URLS that we can input using CSV file?