Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search Windows Search Indexed files with Powershell

Every now and then, I have to locate some files on my network, wich is indexed by the Windows Search service. Entering the pattern on the Explorer search bar get the results in about 10s or so, but when I use the following command, the search usually takes some minutes (2 to 3 min)

gci -Recurse -Filter "VaR*.xlsb"

Is there a way to take advantage of the indexed files database to speed up my search?

like image 544
kb_sou Avatar asked Dec 17 '12 16:12

kb_sou


1 Answers

I Found my answer adapting a vbs script to Powershell, There is the code:

Function Search {
param (  
    [Parameter(ValueFromPipeline = $true)][string]$Path,
    [Parameter(Mandatory=$true)][string]$Pattern)  

    if($Path -eq ""){$Path = $PWD;} 

    $pattern = $pattern -replace "\*", "%"  
    $path = $path + "\%"

    $con = New-Object -ComObject ADODB.Connection
    $rs = New-Object -ComObject ADODB.Recordset

    $con.Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';")
    $rs.Open("SELECT System.ItemPathDisplay FROM SYSTEMINDEX WHERE System.FileName LIKE '" + $pattern + "' AND System.ItemPathDisplay LIKE '" + $path + "'" , $con)

    While(-Not $rs.EOF){
        $rs.Fields.Item("System.ItemPathDisplay").Value
        $rs.MoveNext()
    }
}

This way is blazing Fast. I tested against the answer pointed by David and It was 3x Faster, but this answer only works with indexed files

like image 79
kb_sou Avatar answered Oct 13 '22 05:10

kb_sou