Find SharePoint documents by file type

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

Some time ago I published a blog post with a script to update all instances of a given taxonomy term. The script uses search to find all the relevant items to update based on a search query. At the end, it generates a CSV file with all the items processed.

This script reuses parts of the other script to execute a search query and export results to a CSV file. The search query is using the FileType property to filter results that match the file type specified.

Update: I have published a new blog post with a different approach that queries the libraries on a site. This script takes longer to run, but works with the file types that are not indexed by SharePoint search.

# Variables
$siteUrl = "https://XXXXXXX.sharepoint.com/sites/XXXXXX"
$FileType = "doc*"

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

# ---------------------------------

Connect-PnPOnline -Url $siteUrl -UseWebLogin

$SearchResults = Submit-PnPSearchQuery -Query $query -All -TrimDuplicates $false -SelectProperties ListItemID, Filename 

$results = @()
foreach ($ResultRow in $SearchResults.ResultRows) {
    
    $itemId = $ResultRow["ListItemID"]
    $filename = $ResultRow["Filename"]
    $path = $ResultRow["Path"]
    $parentLink = $ResultRow["ParentLink"]
    Write-Host "Path: $path"

    Write-Host "-------------" -ForegroundColor Yellow

    #Creating object to export in .csv file
    $results += [pscustomobject][ordered] @{
        ItemId     = $itemId 
        Filename   = $filename 
        ParentLink = $parentLink
        Path       = $path
    }

}

$results | Export-Csv -Path $LogFile -NoTypeInformation

2 Replies to “Find SharePoint documents by file type”

  1. Hi Joel,

    Thanks for providing this useful script. How would I be able to customise it so that it runs through my whole SPO tenancy and search for a specific file type in certain document libraries?

    Thanks in advance.
    AG

    1. Hi, sorry for the late reply. Hope you have found a solution, but for future reference to others, this could be done by updating the query. As it’s using search, it’s already covering the entire tenant, you just need to refine the query to be more specific to your own needs (in this case, specific document libraries)

Leave a Reply

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