SharePoint Online: PowerShell to Get a Folder in Document Library

Requirement: Get a Folder in SharePoint Online Document Library using PowerShell.

SharePoint Online: PowerShell to Get a Folder in Document Library

Here is my nifty collection of PowerShell scripts to get a folder in SharePoint Online!

Get a Folder in SharePoint Online using PowerShell By URL

Do you need to get a specific folder from your SharePoint Online document library? Perhaps you want to copy the contents of that folder elsewhere, or maybe you just want to do something with a folder. This post will show you how to use PowerShell to get a folder from a document library.

Here is the SharePoint Online PowerShell to get a folder in the document library. Make sure the folder URL is decoded! E.g., “Shared%20Documents” must be decoded to “Shared Documents”.

#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"

#Variables
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing"
$ServerRelativeUrl= "/Sites/Marketing/Shared Documents/2017"

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 from URL
    $Web = $Ctx.web
    $Ctx.Load($Web)
    $Ctx.executeQuery()

    #Get the Folder object by Server Relative URL
    $Folder = $Web.GetFolderByServerRelativeUrl($ServerRelativeUrl)
    $Ctx.Load($Folder)
    $Ctx.ExecuteQuery() 
    
    #Get Some Folder Properties
    Write-host -f Green "Total Number of Files in the Folder:"$Folder.ItemCount
}
Catch {
    write-host -f Red "Error Getting Folder!" $_.Exception.Message
}

SharePoint Online PowerShell to Get Folder in Document Library By Folder Name

To get a folder list from a library, use this PowerShell:

#Variables
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing"
$LibraryName = "Documents"
$FolderName = "2017"

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 Library
    $Library = $Ctx.web.Lists.GetByTitle($LibraryName)
    $Folders = $Library.RootFolder.Folders
    $Ctx.Load($Folders)
    $Ctx.executeQuery()

    #Get the Folder by Name
    $Folder = $Folders | Where {$_.Name -eq $FolderName}
    $Ctx.Load($Folder)
    $Ctx.ExecuteQuery() 
    
    #Get Some Folder Properties
    Write-host -f Green "Total Number of Files in the Folder:"$Folder.ItemCount
}
Catch {
    write-host -f Red "Error Getting Folder!" $_.Exception.Message
}

SharePoint Online PowerShell to List All Folders in a Library

Are you looking for a quick and easy way to get a list of all the folders in a SharePoint Online document library? If so, PowerShell can help! Let me show you how to use PowerShell to get a list of all the folders in a SharePoint Online document library.

#sharepoint online powershell list folders
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing"
$ListName = "Documents"

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 Library
    $List = $Ctx.web.Lists.GetByTitle($ListName)

    #Query to Get all Folders from the library
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>1</Value></Eq></Where></Query></View>"
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
 
    Write-host "Total Number of Folders in List:" $ListItems.count
   
    #Loop through each file in the library
    Foreach($Item in $ListItems)
    { 
        #Get the Folder Item
        $Ctx.Load($Item.Folder)
        $Ctx.ExecuteQuery()
        Write-host -f Green $Item.Folder.ServerRelativeUrl
    }
}
Catch {
    write-host -f Red "Error Getting Folder!" $_.Exception.Message
}

SharePoint Online PowerShell to Iterate Through Each Folder in a Library:

How about iterating through all folders in a document library from 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 Get Sub-folders of a Folder in SharePoint Online
Function Get-SPOFolders([Microsoft.SharePoint.Client.Folder]$Folder)
{
    Try {
        
        Write-host $Folder.ServerRelativeUrl

        #Process all Sub Folders
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()

        #Iterate through each sub-folder of the folder
        Foreach ($Folder in $Folder.Folders)
        {
            #Call the function recursively
            Get-SPOFolders $Folder
        }
    }
    Catch {
        write-host -f Red "Error Getting Folder!" $_.Exception.Message
    }
}

#Variables
$SiteURL = "https://Crescent.sharepoint.com/Sites/Marketing"
$ListName = "Documents"

#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 Library
$List = $Ctx.web.Lists.GetByTitle($ListName)
$Ctx.Load($List.RootFolder)
$Ctx.ExecuteQuery()

#call the function to get all folders of a document library
Get-SPOFolders $List.RootFolder

PnP PowerShell to Get a Folder in SharePoint Online

Here is the PnP PowerShell to get a folder in SharePoint Document Library! Make sure you have the PnP PowerShell module installed prior to running this script.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ParentFolderURL= "/Shared Documents" #Parent Folder's Site Relative Path
$FolderName="2017"

#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Connect to SharePoint Online site
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
 
    #Get the Folder
    $Folder = Get-PnPFolderItem -FolderSiteRelativeUrl $ParentFolderURL -ItemName $FolderName
    
    Write-host "Total Number of Items in the Folder:" $Folder.ItemCount
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

We can also get a Folder using the Get-PnPFolder cmdlet. Here is an example:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$FolderURL = "/Shared Documents/2018"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the SharePoint library Folder
$Folder = Get-PnPFolder -Url $FolderURL

Get All Folders in a Document Library Recursively

To get all folders and sub-folders from a document library recursively, use the:

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/2018"
$FolderURL = "/Shared Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive  #-Credentials (Get-Credential)

#Get all Sub-Folders of folder
Function Get-SubFolders($FolderURL)
{
    #Get all sub-folders of the Folder
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType Folder | Where {$_.Name -ne "Forms" -and $_.Name -ne "Document"}

    #Loop through the folders
    ForEach($SubFolder in $SubFolders)
    {
        $SubFolderURL = $FolderUrl+"/"+$SubFolder.Name
        write-host -ForegroundColor Green $SubFolder.Name " - " $SubFolderURL
              
        #Call the function recursively
        Get-SubFolders $SubFolderURL
    }
}
  
#Call the function with the root folder of the doc library
Get-SubFolders $FolderURL

PowerShell to Get All Sub-Folders from a Folder in SharePoint Online

To get all folders from a folder, use this PowerShell:

#Parameters
$SiteURL="https://crescent.sharepoint.com/sites/Marketing"
$FolderSiteRelativeURL = "/Branding/2020"

#Connect to the Site collection
Connect-PnPOnline -URL $SiteURL -Interactive

#Get the Folder from site relative URL
$Folder = Get-PnPFolder -Url $FolderSiteRelativeURL

#Get all Subfolders of a folder - recursively
$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder -Recursive

#Get Server relative URL of all subfolders
$SubFolders | Select ServerRelativeURL

How about getting all sub-folders recursively from a given folder?

$SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder -Recursive

Get All Items from a Folder using PnP PowerShell:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$FolderURL= "/Shared Documents/2018" #Folder's Site Relative Path
 
#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
 
    #Get All Items inside the Folder
    $FolderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL
    
    Write-host "Total Number of Items in the Folder:" $FolderItems.Count
    ForEach($Item in $FolderItems)
    {
        Write-host $Item.Name
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Here is an example of counting the number of files and sub-folders on each folder in a SharePoint Online document library: Get Files and Sub-Folders Count on Each Folder in a SharePoint Online Document Library using PowerShell

How do I create a folder in SharePoint Online using PowerShell?

Creating folders in SharePoint Online is a quick and easy way to organize your documents and other content. To create a folder by using PowerShell, connect to SharePoint Online, and run the Add-PnPFolder cmdlet in PnP PowerShell or Folders.Add method in CSOM.
More info: Create a folder in SharePoint Online

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

4 thoughts on “SharePoint Online: PowerShell to Get a Folder in Document Library

  • After running this code

    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 from URL
    $Web = $Ctx.web
    $Ctx.Load($Web)
    $Ctx.executeQuery()

    #Get the Folder object by Server Relative URL
    $Folder = $Web.GetFolderByServerRelativeUrl($ServerRelativeUrl)
    $Ctx.Load($Folder)
    $Ctx.ExecuteQuery()

    #Get Some Folder Properties
    Write-host -f Green “Total Number of Files in the Folder:”$Folder.ItemCount
    }
    Catch {
    write-host -f Red “Error Getting Folder!” $_.Exception.Message
    }

    #Read more: https://www.sharepointdiary.com/2018/03/sharepoint-online-powershell-to-get-folder-in-document-library.html#ixzz7QVJ6efdi

    I am getting the error mention below can you please help me in this

    Error Getting Folder! Cannot find type [Microsoft.SharePoint.Client.SharePointOnlineCredentials]: verify that the assembly containing this type is loaded.

    Reply
    • Download and install either the SharePoint Online CSOM assemblies or SharePoint Online Management Shell on your client machine!

      Reply
  • In SharePoint site any ways to get hidden folder location by using PnP Powershell script.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *