SharePoint Online: Find All Documents using Keyword Query PowerShell

Requirement: Get All Documents in SharePoint Online Site and Export them to CSV.

PowerShell to Find All Documents using Keyword Query in SharePoint Online

SharePoint Online provides the Keyword Query Language (KQL), a powerful and flexible syntax for building search queries. Here is an example of using KQL in SharePoint Online: A user wants to get a list of all documents not updated in recent years. So, the PowerShell script to get all documents in a SharePoint Online site collection sorted by “Last Modified Date”.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Search.dll"

#Config Variables
$SearchQuery= "path: AND IsDocument:true AND (NOT FileType:aspx)"
$CSVFile = "C:\Temp\SearchResults.csv"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    #Define Keyword
    $KeywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($Ctx)
    $KeywordQuery.QueryText = $SearchQuery
    $KeywordQuery.RowLimit  = 500

    #Execute Search        
    $SearchExecutor = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($Ctx)
    $SearchResults = $SearchExecutor.ExecuteQuery($KeywordQuery)

    Write-host "Search Results Found:"$SearchResults.Value[0].ResultRows.Count

    #Get Search Results
        $Results = @()
        foreach($Result in $SearchResults.Value[0].ResultRows)
            $Results += New-Object PSObject -Property @{
                        'Document Name' =  $Result["Title"]
                        'URL' = $Result["Path"]
                        'Created By' = $Result["CreatedBy"]  
                        'Last Modified' = $Result["LastModifiedTime"]               
        #Export search results to CSV
        $Results | Export-Csv $CSVFile -NoTypeInformation
        Write-Host -f Green "Search Results Exported to CSV File!"
Catch {
    write-host -f Red "Error Getting Search Results!" $_.Exception.Message


keyword query language sharepoint online

By using KQL in SharePoint Online, you can create complex and targeted search queries to find the information you need quickly and easily.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “SharePoint Online: Find All Documents using Keyword Query PowerShell

  • what is the criteria to fetch 500 items, is it random? I get 500 row of result but not sure why this has been pulled. How do we get list of all documents from whole tenant?

      • Is there a script to get list of all files from all sites in tenant with Last modified date?

        what is lastmodified time in this script?


Leave a Reply

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