Find SharePoint documents by file extension

This blog post contains a simple PowerShell script that you can use to find all SharePoint files from a certain file extension. It creates a CSV file with the results, so you can easily consume or manipulate that information. 

On my previous blog post, I shared a script to find documents by file type using search. You can read more here. This approach works well for file types that are indexed by search, but this is not always the case. 

This script addresses that limitation as it parses all files across all document libraries. It checks if the file name ends with the extension specified and adds the item to a CSV file if so.

# Variables
$siteUrl = "https://XXXXXXXX.sharepoint.com/sites/XXXX"
$FileType = "lnk"

$LogFile = "C:\users\$env:USERNAME\Desktop\File Type - $FileType.csv"

#Connect to SPO
Connect-PnPOnline -Url $siteUrl -UseWebLogin

#Store in variable all the document libraries in the site
$DocLibrary = Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 } 

$results = @()
foreach ($DocLib in $DocLibrary) {

    #Get list of all items in the document library
    $AllItems = Get-PnPListItem -PageSize 1000 -List $DocLib -Fields "ID"
    
    #Loop through each files/folders in the document library for folder size = 0
    foreach ($Item in $AllItems) {
        if ($Item["FileLeafRef"] -like "*.$FileType") {

            Write-Host "Item:" $Item["FileRef"] -ForegroundColor Yellow
                
            #Creating object to export in .csv file
            $results += [pscustomobject][ordered] @{
                ID       = $Item["ID"] 
                FileName = $Item["FileLeafRef"] 
                FilePath = $Item["FileRef"]
            }
            
        }#end of IF statement
    }
}
$results | Export-Csv -Path $LogFile -NoTypeInformation

6 Replies to “Find SharePoint documents by file extension”

  1. Hello Mr. Joel Rodrigues,

    I am, trying to use your great scripts. This is working perfectly but the out file is not giving only selected files, it is giving all types of files
    Regards
    Abul Hasnat

    1. Hi Abul,
      What are you setting the $FileType variable to and are you able to check what is within $Item[“FileLeafRef”] for each file?
      This is a simple comparison of the file extension, so could be a simple issue

  2. Hi Joel
    Is there a way to search for multiple file types

    i tried the following

    $FileType = @(“mp4″,”exe”)

    1. Maybe try relying on the FileType property instead and check for each file if the file type exists within an array of types to search for

  3. Hi Joel,

    This is great. Do you happen to know what permissions the user needs to have to the SharePoint site to be able to run this successfully?

    Thanks,

    1. Hi Mike, sorry for the late reply. Read access on the site and the list should be enough. Also, please note that the script is a bit old and -UseWebLogin to connect should be replaced with -Interactive or one of the other modern options

Leave a Reply

Your email address will not be published. Required fields are marked *