PowerShell Windows Index Search

    I got asked to build a PowerShell query to search the Windows Index.  Basically, the idea is that we could search for multiple files at once, with different extensions, and perhaps streamline the process over the GUI index search function. 


    Here's the actual code:

    clear
    $path = 'C:\Users\Vapor'
    $sql = "SELECT System.ItemName, System.ItemPathDisplay, System.ItemType FROM SYSTEMINDEX `
    WHERE SCOPE = '$path' AND CONTAINS(System.Search.Contents,'Seven') AND CONTAINS(System.FileName,'*.docx') `
    OR CONTAINS(System.Search.Contents,'Layers') AND CONTAINS(System.FileName,'*.xlsx')"

    $provider = "provider=search.collatordso;extended properties='application=windows';"
    $connector = new-object system.data.oledb.oledbdataadapter -argument $sql, $provider
    $dataset = new-object system.data.dataset

    if ($connector.fill($dataset)) { $dataset.tables[0] | select-object System.ItemName, System.ItemPathDisplay, `
    System.ItemType  | format-table -autosize * }

    We define our path, we're performing an AND OR SQL query, and we get two different searches combined into one. 

    This was just a working example -- the recipient is savvy enough to add or subtract to their liking.  I thought it was kind of cool because I've searched for files with PowerShell but this is the first time I've leveraged the index. 

    Time to rewrite some scripts. 



    © 2020 sevenlayers.com