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

Requirement: Get a 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 library. This blog post will show you how to use PowerShell to iterate through all the files in a document library. This can be useful if you need to perform some operation or collect data from all the files in the library, or if you want to get a list of all the files in the library.

Here is the PowerShell to iterate 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 Iterate through All Files and Folders of a Document Library

Let’s loop through each folder and get files from a given library with PnP PowerShell:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
$FolderSiteRelativeUrl = "/shared documents"

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

#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    

If you want to get all files from a SharePoint Online document library, use: PowerShell to Get All Files from a SharePoint Online Document Library

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

6 thoughts on “SharePoint Online: PowerShell to Iterate Through All Files in a Document Library

  • Hi Salaudeen,

    I am trying to extract a report on the item last modified date in all the libarary ,iterate through in each libarary of all the sitecollection and subsite in sharepoint online via powershell..also wanted to have a filter on last modified date would be 5 years from now should be avaible in the export.

    I tired to joined many of your scripts together some how ,not getting the expected reult . would you help please here!!

    i tired below and did not work as expected .
    #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
    $AdminSiteURL =”https://ameriprise-admin.sharepoint.com”
    $CSVFile = “C:\temp\SiteData2.csv”

    #Get Credentials to connect
    $Cred= Get-Credential

    #Connect to Admin Center
    Connect-SPOService -Url $AdminSiteURL -Credential $Cred

    #Get All Site Collections
    $Sites = Get-SPOSite | -Limit All

    $SiteData_a = @()

    #Loop through each site collection
    ForEach($Site in $Sites)
    {
    Write-host -f Yellow “Getting Data for Site:” $Site.URL
    Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Site.URL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get the Root Web
    $Web = $Ctx.web
    $Ctx.Load($Web)
    $Ctx.ExecuteQuery()

    #Loop through each subsite and get subsite URL
    ForEach ($Subweb in $web.Webs)
    {
    Write-host $Subweb.url
    }

    #Get all lists from the Web
    $Lists = $Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    #Iterate through Lists
    ForEach($List in $Lists | Where {$_.BaseType -eq “DocumentLibrary” -and $_.hidden -eq $false})
    {
    #Get List last modified date
    Write-Host -f Cyan $List.Title “Last Modified: ” $List.LastItemUserModifiedDate

    #Define CAML Query to Filter
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXML = ”

    #Get List Items

    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    Write-host -f Green “Number of List Items Found:”$ListItems.Count
    #Get Each Item’s Created Date
    $ListItems | ForEach-Object {
    Write-host -f Magenta (“List Item:{0} was Modified on {1}” -f $_[“FileLeafRef”],$_[“Modified”])

    #Get the last modified date of the site
    #$SiteData_a += New-Object PSObject -Property @{
    $Siteabcd = New-Object PSObject -Property @{
    SiteTitle = $Web.Title
    SiteURL = $Web.Url
    LibararyName=$List.Title
    ItemName= $_[“FileLeafRef”]
    Itemmodifieddate = $_[“Modified”]
    ListModifiedDate =$List.LastItemUserModifiedDate
    SiteLastModified= $Web.LastItemUserModifiedDate

    }
    }
    Write-host “testing sitedata: $Siteabcd”
    $SiteData_a = $SiteData_a + $Siteabcd

    }
    }
    Catch {
    write-host -f Red “Error:” $_.Exception.Message
    }
    }
    #Export the results to CSV
    $SiteData_a | Export-CSV $CSVFile -NoTypeInformation

    Reply
  • Hi …how can this same functionality be achieved in Sharepoint on premise. Any links would be helpful. Thanks

    Reply
  • Hi Salaudeen Rajack – With help of this script, i completed my requirement. It’s really helped me.

    Thank you very very much!!

    Reply
  • How export all above data to excel

    Reply

Leave a Reply

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