SharePoint Online: Get All Files from a Folder using PowerShell

Requirement: SharePoint Online PowerShell to list files in a folder

PowerShell to Get Files from Folder in SharePoint Online:

Have you ever needed to get all the files from a folder in SharePoint Online? Maybe you are migrating files to a different location and need to grab everything from the folder. Or, perhaps you just want a quick way to get all the files from a given folder without having to navigate through each one. Whatever your reason, PowerShell can help! In this blog post, I will show you how to use PowerShell to get all the files from a given folder in SharePoint Online.

Let’s retrieve all files from a given folder in the SharePoint Online document library using 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 Get-FilesFromFolder()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $FolderURL
    )
    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 Folder and Files
        $Folder=$Ctx.Web.GetFolderByServerRelativeUrl($FolderURL)
        $Ctx.Load($Folder)
        $Ctx.Load($Folder.Files)
        $Ctx.ExecuteQuery()

        #Iterate through each File in the folder
        Foreach($File in $Folder.Files)
        {
            #Get Name for each File
            Write-Host $File.Name
        }
    }
    Catch {
        write-host -f Red "Error Getting Files from Folder!" $_.Exception.Message
    }
}

#Set Parameter Values
$SiteURL="https://crescent.sharepoint.com"
$FolderURL="/Shared Documents/Documentation"

#Call the function to get list items from folder
Get-ListItemsFromFolder -SiteURL $SiteURL -FolderURL $FolderURL

This SharePoint Online PowerShell gets all files from the folder.

PnP PowerShell to Get All Files from a Folder in SharePoint Online:

To get all files from a folder recursively, we can use this PowerShell script:

#Config Variables
$SiteURL = "https://crescentIntranet.sharepoint.com/sites/Marketing"
$ListName ="Branding"
$FolderServerRelativePath = "/sites/Marketing/Branding*" #Any file under the given path
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get all files from folder
Get-PnPListItem -List $ListName -PageSize 2000 | Where {$_.FieldValues.FileRef -like $FolderServerRelativePath -and $_.FileSystemObjectType -eq "File" } | ForEach-Object {
   Write-host $_.FieldValues.FileRef
}

Instead of getting all items and then applying a filter, we can use Get-PnPFolderItem cmdlet to get all files from a sub-folder in SharePoint Online

#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$FolderURL= "/Shared Documents/2017" #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 Files from the Folder
    $FolderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType File
    
    Write-host "Total Number of Files in the Folder:" $FolderItems.Count
    ForEach($File in $FolderItems)
    {
        $File.Name
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Here is my another PowerShell to get all items from a SharePoint Online folder: SharePoint Online: Get All Items in a Folder

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

3 thoughts on “SharePoint Online: Get All Files from a Folder using PowerShell

Leave a Reply