kwizcom banner advertisement

SharePoint Online: Get All Files from a Folder using PowerShell

Requirement: SharePoint Online PowerShell to list files in folder.

PowerShell to Get Files from Folder in SharePoint Online:
Lets retrieve all files from a given folder in 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 folder. here is my another post SharePoint Online: Get All Items in a Folder

PnP PowerShell to Get All Files from a 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
}
SharePoint Online: Get All Files from a Folder using PowerShell SharePoint Online: Get All Files from a Folder using PowerShell Reviewed by Salaudeen Rajack on May 23, 2017 Rating: 5

3 comments:

  1. Doesn't this script require visual studio? Where else do you get the csom binaries from?

    ReplyDelete
    Replies
    1. You don't need Visual studio. Just download and install CSOM SDK to get the assembly (.dll) files from https://www.microsoft.com/en-us/download/details.aspx?id=42038

      Delete
    2. Great tip. Thank you for your sharing

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.