Query the Windows Search Index with SQL

Old SQL Server examples sometimes create an Indexing Service linked server and query its catalog through OPENQUERY. That provider belongs to a retired Windows component. Where Windows Search is available, query its SystemIndex catalog through the Windows Search OLE DB provider instead. This is Windows Search SQL, not a Transact-SQL query to paste into an ordinary SQL Server connection.

Start with a Read-Only Local Query

Windows Search must be installed and running, and the location must be indexed. A file can appear by metadata even when its contents have not been indexed. Begin by listing a small number of indexed file items:

SQL
SELECT TOP 10 System.ItemName, System.ItemUrl
FROM SystemIndex
WHERE SCOPE = 'file:'
ORDER BY System.ItemName

The exact rows depend on your index and current user's access. An empty result does not prove that no files exist on disk.

Execute through the Search Provider

The following Windows PowerShell example uses the installed Windows Search OLE DB provider through ADO. It reads the index and does not change files or indexing configuration.

PowerShell
$connection = New-Object -ComObject ADODB.Connection
$recordset = $null
try {
    $connection.Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';")
    $query = @'
SELECT TOP 10 System.ItemName, System.ItemUrl
FROM SystemIndex
WHERE SCOPE = 'file:'
ORDER BY System.ItemName
'@
    $recordset = $connection.Execute($query)
    while (-not $recordset.EOF) {
        [pscustomobject]@{
            Name = $recordset.Fields.Item('System.ItemName').Value
            Url = $recordset.Fields.Item('System.ItemUrl').Value
        }
        $recordset.MoveNext()
    }
}
finally {
    if ($null -ne $recordset) {
        if ($recordset.State -ne 0) { $recordset.Close() }
        [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($recordset)
    }
    if ($connection.State -ne 0) { $connection.Close() }
    [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($connection)
}

Expect up to ten name/URL pairs. A provider error means you should verify Windows Search availability and the installed provider, rather than install the old Indexing Service provider or enable SQL Server linked-server options.

Restrict a Remote Query Where Supported

Microsoft documents a remote catalog form that names the remote computer and an accessible UNC-backed scope. Replace the fictional computer and share below and use this query through the same search provider:

SQL
SELECT TOP 20 System.ItemName, System.ItemUrl
FROM SearchHost.SystemIndex
WHERE SCOPE = 'file://SearchHost/SharedDocs'
  AND CONTAINS('calibration')
ORDER BY System.ItemName

Remote querying requires a compatible remote Windows Search service, indexed shared content and read access for the caller. The remote computer and scope must agree. Treat those as prerequisites to verify on your Windows deployment, not as a promise that every SMB share or Windows Server installation exposes a search catalog. Do not loosen share permissions or network controls just to return results.

Keep the Search Boundaries Clear

The provider is read-only, and its property names and SQL dialect differ from SQL Server. Full-text content matching also depends on indexing state and installed file filters. If an application needs to join search results to relational rows, retrieve the search results through the appropriate client component and pass reviewed identifiers to SQL Server. SQL Server Full-Text Search is a separate option for text stored in the database, not a drop-in remote filesystem crawler.

Microsoft References

Author: Herman Schoenfeld