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

    #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

    #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()
        [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

        #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= ""
$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.
$SiteURL = ""
$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
    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
        $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


Please Login and comment to get your questions answered!

Powered by Blogger.