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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With