How to get OneDrive and SharePoint file path using PowerShell

Aase Nomad 246 Reputation points
2022-08-05T16:31:07.137+00:00

I've this PowerShell Script that get the folder name, Folder path and last modified date from either SharePoint Online or OneDrive account but I'm just wondering how can I also get the the file name inside each folder with with their path and last modified date also?.

I've stuck with this so any help or suggestion would be really appreciated.

   Connect-PnPOnline $SiteURL -Credentials $credential  
           
   #Get the list  
   $List = Get-PnPList -Identity $ListName  
          
   #Get Folders from the Library - with progress bar  
   $global:counter = 0  
         
   $FolderItems = Get-PnPListItem -List $ListName -PageSize 500 -Fields FileLeafRef -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete `  
       ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Items from List:" -Status "Processing Items $global:Counter to $($List.ItemCount)"; }  | Where { $_.FileSystemObjectType -eq "Folder" }  
         
         
   Write-Progress -Activity "Completed Retrieving Folders from List $ListName" -Completed  
          
   $FolderStats = @()  
   ForEach ($FolderItem in $FolderItems) {  
       #Get Files and Folders of the Folder  
       Get-PnPProperty -ClientObject $FolderItem.Folder -Property Files, Folders | Out-Null  
              
       #Collect data  
       $Data = [PSCustomObject][ordered]@{  
           FolderName     = $FolderItem.FieldValues.FileLeafRef  
           Path            = $FolderItem.FieldValues.FileRef  
           ModifiedDate    = $FolderItem.FieldValues.Modified  
       }  
     
       $Data  
       $FolderStats += $Data  
   }  
   $FolderStats | Export-Csv -Path $CSVFile -NoTypeInformation  
Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
Microsoft 365 and Office OneDrive For business Windows
Microsoft 365 and Office SharePoint Server Development
Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Yi Lu_MSFT 17,611 Reputation points
    2022-08-21T09:12:06.827+00:00

    Hi @Aase Nomad
    You could use the following script to get the files in a folder:

    #Config Variables  
    $SiteURL = "https://crescent.sharepoint.com/sites/Funds"  
    $ListName ="Funds"  
    $FolderServerRelativePath = "/sites/Funds/NeoFunds/EAEF IV*" #Any file under the given path    
    $ReportOutput = "C:\Temp\EAEFIV-Inventory.csv"  
    $Pagesize = 500  
    #Array to store results  
    $Results = @()  
       
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -UseWebLogin  
    $List  = Get-PnPList -Identity $ListName  
       
    $global:counter = 0;  
    $ListItems = Get-PnPListItem -List $ListName -PageSize $Pagesize -Fields Author, Editor, Created, File_x0020_Type -ScriptBlock `  
         { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `  
            "Getting Documents from Library '$($List.Title)'" -Status "Getting Documents data $global:Counter of $($List.ItemCount)";} | Where {$_.FieldValues.FileRef -like $FolderServerRelativePath}  
       
    $ItemCounter = 0  
    #Iterate through each item  
    Foreach ($Item in $ListItems)  
    {  
            $Results += New-Object PSObject -Property ([ordered]@{  
                Name              = $Item["FileLeafRef"]  
                Type              = $Item.FileSystemObjectType  
                FileType          = $Item["File_x0020_Type"]  
                RelativeURL       = $Item["FileRef"]  
                CreatedByEmail    = $Item["Author"].Email  
                CreatedOn         = $Item["Created"]  
                Modified          = $Item["Modified"]  
                ModifiedByEmail   = $Item["Editor"].Email  
            })  
        $ItemCounter++  
        Write-Progress -PercentComplete ($ItemCounter / ($ListItems.Count) * 100) -Activity "Exporting data from Documents $ItemCounter of $($ListItems.Count)" -Status "Exporting Data from Document '$($Item['FileLeafRef'])"         
    }  
         
    #Export the results to CSV  
    $Results | Export-Csv -Path $ReportOutput -NoTypeInformation  
          
    Write-host "Folder Inventory Exported to CSV Successfully!" -f Green  
    

    For more information, you could refer to:
    https://www.sharepointdiary.com/2017/02/sharepoint-online-get-list-items-from-folder-using-powershell.html

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    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.

    0 comments No comments

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.