M365 Dev Blog

Find SharePoint documents by file type

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
Exit mobile version