SharePoint Online: Check If Folder Exists using PowerShell

Requirement: Check if folder exists in SharePoint Online document library.
Check If Folder Exists in SharePoint Online using PowerShell

SharePoint Online: PowerShell to Check If Folder Exists
Here is the PowerShell for SharePoint Online to check if 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()
        [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

        #Check Folder Exists
        Try { 
            $Folder = $Web.GetFolderByServerRelativeUrl($FolderRelativeURL)

            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
$FolderRelativeURL="/Shared Documents/2018"

#Call the function 
Check-SPOFolderExists -SiteURL $SiteURL -FolderRelativeURL $FolderRelativeURL 
Alternatively, you can use the other method used in my another article: SharePoint Online: Create a Folder using PowerShell

1 comment:

  1. is there a way to use this script to loop through all the list of site URLS that we can input using CSV file?


Please Login and comment to get your questions answered!

Powered by Blogger.