SharePoint Online: PowerShell to Iterate Through All Files in a Document Library

Requirement: Get List of Files in a SharePoint Online Document Library using PowerShell

SharePoint Online PowerShell to Iterate through All Files in a Document Library:
Often in PowerShell scripting, we may have to loop through each file in a SharePoint Online libraries. This PowerShell script iterates through each folder of the library and lists all files.
#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 all files of a folder
Function Get-FilesFromFolder([Microsoft.SharePoint.Client.Folder]$Folder)
{
    Write-host -f Yellow "Processing Folder:"$Folder.ServerRelativeUrl

    #Get All Files of the Folder
    $Ctx.load($Folder.files)
    $Ctx.ExecuteQuery()

    #list all files in Folder
    ForEach ($File in $Folder.files)
    {
        #Get the File Name or do something
        Write-host -f Magenta $File.Name
    }
        
    #Recursively Call the function to get files of all folders
    $Ctx.load($Folder.Folders)
    $Ctx.ExecuteQuery()

    #Exclude "Forms" system folder and iterate through each folder
    ForEach($SubFolder in $Folder.Folders | Where {$_.Name -ne "Forms"})
    {
        Get-FilesFromFolder -Folder $SubFolder
    }
}

#powershell list files in sharepoint online library
Function Get-SPODocLibraryFiles()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $LibraryName
    )
    Try {
        #Setup Credentials to connect
        $Cred = Get-Credential
        $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Get the Library and Its Root Folder
        $Library=$Ctx.web.Lists.GetByTitle($LibraryName)
        $Ctx.Load($Library)
        $Ctx.Load($Library.RootFolder)
        $Ctx.ExecuteQuery()

        #Call the function to get Files of the Root Folder
        Get-FilesFromFolder -Folder $Library.RootFolder

     }
    Catch {
        write-host -f Red "Error Getting Files from Library!" $_.Exception.Message
    }
}
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com"
$LibraryName="Project Docs"

#Call the function to Get All Files from a document library
Get-SPODocLibraryFiles -SiteURL $SiteURL -LibraryName $LibraryName 

SharePoint Online PowerShell to list all documents:
SharePoint Online loop through all files in a Document Library using powershell

PnP PowerShell to Get Iterate through All Files and Folders of a Document Library 
Let's loop through each folder, sub-folder, and files of a given library.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
$FolderSiteRelativeUrl = "/shared documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Function to get all files from a folder
Function Get-SPOFilesFromFolder
{
    [cmdletbinding()]
    param
    (
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Client.Folder]$Folder
    )
    Write-host "$($Folder.Name) ($($Folder.ServerRelativeUrl))" -f Yellow
    
    #Get Sub-folders of the folder
    Get-PnPProperty -ClientObject $Folder -Property ServerRelativeUrl, Folders | Out-Null    
    
    #calculate the FolderSiteRelativeUrl of the Folder
    If($Web.ServerRelativeUrl -eq "/")
    {
        $FolderSiteRelativeUrl = $Folder.ServerRelativeUrl
    }
    Else
    {        
        $FolderSiteRelativeUrl= $Folder.ServerRelativeUrl.Replace($Web.ServerRelativeUrl,'')
    }
    
    #Get All Files of the Folder
    $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeUrl -ItemType File 
    $Files | ForEach-Object {
        Write-Host "`t$($_.Name)"
    }

    #Process all Sub-folders - Exclude "Forms" and Hidden folders
    ForEach($SubFolder in $Folder.Folders | Where {($_.Name -ne "Forms") -and (-Not($Folder.Name.StartsWith("_")))})
    {
        Get-SPOFilesFromFolder -Folder $SubFolder
    }
}
$Web = Get-PnPWeb
$Library =  Get-PnPList -Identity $ListName -Includes RootFolder
Get-SPOFilesFromFolder $Library.RootFolder

5 comments:

Please Login and comment to get your questions answered!

Powered by Blogger.