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
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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.