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 folder SharePoint Online:

Get Folder in SharePoint Online using PowerShell By URL:
Here is the SharePoint Online PowerShell to get folder in document library
#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://crescenttech.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
#Variables
$SiteURL = "https://crescenttech.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
#sharepoint online powershell list folders
$SiteURL = "https://crescenttech.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:
#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://crescenttech.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 for SharePoint Online to get a folder in Document Library
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ParentFolderURL= "/Shared Documents" #Folder's Site Relative Path
$FolderName="2017"
 
#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Connect to PNP Online
    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 Get-PnPFolder cmdlet. Here is an example:
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$FolderURL = "/Shared Documents/2018"

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

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

Get All Folders in a Document Library Recursively:
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/2018"
$FolderURL = "/Shared Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin  #-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
Get-SubFolders $FolderURL


Get All Items from a Folder using PnP PowerShell:
#Config Variables
$SiteURL = "https://crescenttech.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
}
SharePoint Online: PowerShell to Get a Folder in Document Library SharePoint Online: PowerShell to Get a Folder in Document Library Reviewed by Salaudeen Rajack on March 18, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.