How to export each file and folder URL via PowerShell or Graph API from SharePoint Online? Is there any such way we can export from whole tenant and give it to users?

Vinod Survase 4,776 Reputation points
2023-11-28T12:41:39.86+00:00

How to export each file and folder URL via PowerShell or Graph API from SharePoint Online? Is there any such way we can export from whole tenant and give it to users?

Microsoft 365 and Office Install, redeem, activate For business Windows
Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Xyza Xue_MSFT 30,176 Reputation points Microsoft External Staff
    2023-11-29T02:00:41.2266667+00:00

    Hi @Vinod Survase ,

    You can Get All Documents file and folder URL from a SharePoint Online Site using PnP PowerShell.

    Please use the following code:

    #Parameters
    $SiteURL = "https://yourdomain.sharepoint.com/sites/xyzasite1"
    $CSVPath = "C:\Temp\DocumentInventory.csv"
    $global:DocumentInventory = @()
    $Pagesize = 2000
      
    #Function to scan and collect Document Inventory
    Function Get-DocumentInventory
    {
         [cmdletbinding()]
        param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $Web)
        
        Write-host "Getting Documents Inventory from Site '$($Web.URL)'" -f Yellow
        Connect-PnPOnline -Url $Web.URL -Interactive
     
        #Calculate the URL of the tenant
        If($Web.ServerRelativeUrl -eq "/")
        {
            $TenantURL = $Web.Url
        }
        Else
        {
            $TenantURL = $Web.Url.Replace($Web.ServerRelativeUrl,'')
        }
      
        #Exclude certain libraries
        $ExcludedLists = @("Form Templates", "Preservation Hold Library","Site Assets", "Pages", "Site Pages", "Images",
                                "Site Collection Documents", "Site Collection Images","Style Library")
                                    
        #Get All Document Libraries from the Web
        Get-PnPList -PipelineVariable List | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $_.Title -notin $ExcludedLists -and $_.ItemCount -gt 0} | ForEach-Object {
            #Get Items from List  
            $global:counter = 0;
            $ListItems = Get-PnPListItem -List $_ -PageSize $Pagesize -Fields Author, Created, File_x0020_Type,File_x0020_Size -ScriptBlock `
                     { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($_.ItemCount) * 100) -Activity "Getting Documents from '$($_.Title)'" -Status "Processing Items $global:Counter to $($_.ItemCount)";} | Where {$_.FileSystemObjectType -eq "File"}
            Write-Progress -Activity "Completed Retrieving Documents from Library $($List.Title)" -Completed
          
                #Get Root folder of the List
                $Folder = Get-PnPProperty -ClientObject $_ -Property RootFolder
      
                #Iterate through each document and collect data          
                ForEach($ListItem in $ListItems)
                { 
                    #Collect document data
                    $global:DocumentInventory += New-Object PSObject -Property ([ordered]@{
                        SiteName  = $Web.Title
                        SiteURL  = $Web.URL
                        LibraryName = $List.Title
                        ParentFolder = $Folder.ServerRelativeURL
                        FileName = $ListItem.FieldValues.FileLeafRef
                        FileType = $ListItem.FieldValues.File_x0020_Type
                        FileSize = [math]::Round($ListItem.FieldValues.File_x0020_Size/1KB)
                        AbsoluteURL = "$TenantURL$($ListItem.FieldValues.FileRef)"
                        CreatedBy = $ListItem.FieldValues.Author.Email
                        CreatedAt = $ListItem.FieldValues.Created
                        ModifiedBy = $ListItem.FieldValues.Editor.Email
                        ModifiedAt = $ListItem.FieldValues.Modified
                    })
                }
            }
    }
       
    #Connect to Site collection
    Connect-PnPOnline -Url $SiteURL -Interactive
         
    #Call the Function for Webs
    Get-PnPSubWeb -Recurse -IncludeRootWeb | ForEach-Object { Get-DocumentInventory $_ }
        
    #Export Documents Inventory to CSV
    $Global:DocumentInventory | Export-Csv $CSVPath -NoTypeInformation
    Write-host "Documents Inventory Report has been Exported to '$CSVPath'" -f Green
    
    
    

    Here are the results of the run in my environment:

    User's image


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.