Get URL of All Files in SharePoint Online Document Library using PowerShell

Requirement: Get the URL of All Files in a SharePoint Online Document Library using PowerShell

Get URLs of All Files in a SharePoint Online Document Library using PowerShell CSOM

Are you looking for a PowerShell script to get the URL of all files from a folder in SharePoint Online? Well, Here is the PowerShell script to get a list of all the files and their URLs in a specific folder using the client-side object model.

#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"
    
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Documents"
  
#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

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

    #Get All Items from the List
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
           
    $DataCollection = @()
    #Loop through each item 
    ForEach($ListItem in $ListItems)
    {
        #Calculate Absolute URL
        If($Web.ServerRelativeUrl -eq "/")
        {
            $AbsoluteURL=  $("{0}{1}" -f $Web.Url, $ListItem.FieldValues["FileRef"])
        }
        else
        {
            $AbsoluteURL=  $("{0}{1}" -f $Web.Url.Replace($Web.ServerRelativeUrl,''), $ListItem.FieldValues["FileRef"])
        } 

        #Collect data        
        $Data = New-Object PSObject -Property @{
            FileName  = $ListItem.FieldValues["FileLeafRef"]
            RelativeURL = $ListItem.FieldValues["FileRef"]
            AbsoluteURL = $AbsoluteURL
        }
        $DataCollection += $Data
    }
    $DataCollection | Format-List
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}

PnP PowerShell to Get File URL in SharePoint Online

If you’re looking for a PowerShell script to gather the URLs of all files in a given folder in SharePoint Online, look no further! Let me show you how to use PnP PowerShell to get URLs of all files in a folder quickly.

#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get All Items from the list - In batches of 500
$ListItems = Get-PnPListItem -List $ListName -PageSize 500

#Loop through List Items and Get File URL
[email protected]()
ForEach($Item in $ListItems)
{
    $Results += New-Object PSObject -Property @{
    FileName = $Item.FieldValues['FileLeafRef']
    FileURL = $Item.FieldValues['FileRef']
    }
}
$Results

Here is another post: How to use PowerShell to get all the files from a folder in SharePoint Online?

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!

One thought on “Get URL of All Files in SharePoint Online Document Library using PowerShell

  • These are good examples of how to use Powershell to query SharePoint lists. Do you have anything that can also test to see whether the links stored in document libraries (this are “.url” files) are valid or not?

    Reply

Leave a Reply