Search for File Content using Windows Desktop Search


posted by Tobias
06-29-2011

Downloads: 404
File size: 1.1kB
Views: 2,389

Embed
Search for File Content using Windows Desktop Search
  1. # search for all docx files in a given folder (or its subfolders) and return paths to all 
  2. # those documents that contain either "PowerShell" or "Tip"... 
  3.  
  4. # by Dr. Tobias Weltner   
  5. # PowerShell Trainings throughout Europe   
  6. # Hungry for a training? Get in touch: tobias.weltner (AT) email.de   
  7.  
  8. # More info: http://207.46.16.252/en-us/library/ff404235.aspx 
  9.  
  10. $keyword1 = "PowerShell" 
  11. $keyword2 = "Tip" 
  12. $extension = ".docx" 
  13. $folder = 'C:\Users\Tobias\MyDocuments\TipDocs' 
  14.  
  15. $objConnection = New-Object -com ADODB.Connection 
  16. $objRecordSet = New-Object -com ADODB.Recordset 
  17. $objConnection.Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
  18. $objRecordSet.Open("SELECT System.ItemPathDisplay FROM SYSTEMINDEX WHERE System.FileExtension = '$extension' AND (Contains(Contents,'$keyword1') OR Contains(Contents, '$keyword2')) AND System.ItemPathDisplay LIKE '$folder\%'", $objConnection
  19. if ($objRecordSet.EOF -eq $false) {$objRecordSet.MoveFirst() } 
  20.  
  21. while ($objRecordset.EOF -ne $true) { 
  22.   $objRecordset.Fields.Item("System.ItemPathDisplay").Value 
  23.   $objRecordset.MoveNext() 
illustrates how you can use PowerShell to query the desktop search engine to return paths to all docx files that contain specific keywords. You can easily adjust the script to search for videos, music or other information as well.
Copyright 2012 PowerShell.com. All rights reserved.