PowerShell: Querying Windows Desktop Search

by Doug Finke on December 21, 2008

in PowerShell, Windows Desktop Search

I saw this IronPython: Querying Windows Desktop Search.

Then took Lee Holmes’ Invoke-SqlCommand and hacked up Invoke-WDS 

param(
    $sqlCommand = "SELECT FileName FROM SYSTEMINDEX Where System.FileExtension ='.ps1'  Order By FileName"
)
 
$connectionString = "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
 
## Connect to the data source and open it
$connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
$command = New-Object System.Data.OleDb.OleDbCommand $sqlCommand,$connection
$connection.Open()
 
## Fetch the results, and close the connection
$adapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
$dataset = New-Object System.Data.DataSet
[void] $adapter.Fill($dataSet)
$connection.Close()
 
## Return all of the rows from their query
$dataSet.Tables | Select-Object -Expand Rows

The Script Center has a deeper treatment here Seek and Ye Shall Find Scripting Windows Desktop Search 3.0

You can create queries like

@"
 SELECT 
  System.FileName 
 FROM 
  SYSTEMINDEX 
 WHERE 
  System.Music.Artist = 'John Lennon' 
 OR
  System.Music.Artist = 'George Harrison'
"@