{"id":18211,"date":"2010-05-30T00:01:00","date_gmt":"2010-05-30T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/05\/30\/hey-scripting-guy-weekend-scripter-using-the-windows-search-index-to-find-specific-files\/"},"modified":"2010-05-30T00:01:00","modified_gmt":"2010-05-30T00:01:00","slug":"hey-scripting-guy-weekend-scripter-using-the-windows-search-index-to-find-specific-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-using-the-windows-search-index-to-find-specific-files\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Using the Windows Search Index to Find Specific Files"},"content":{"rendered":"<p><span style=\"font-size: small\"><img decoding=\"async\" height=\"16\" width=\"125\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" alt=\"Bookmark and Share\"><\/span>\n<span style=\"font-size: small\">&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: small\">Microsoft Scripting Guy Ed Wilson here. One of the cool things about using Windows PowerShell is the different objects with which you can interact. Windows Search is an example of this. By using ADO, you can query the Windows Search index and retrieve information about files. This technique can have some very interesting possibilities. Because querying the Windows Search index is so fast, a function could be written that would search for specific files that might be required in a script. You could also use this in an attempt to locate a missing file. <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: small\">The SearchSpecificWindowsSearch.ps1 script uses COM-based ADO objects to query the <b>SystemIndex<\/b> for files that are larger than 10,000,000,000 bytes. Specific properties of the matching files are then displayed in the output window. The complete SearchSpecificWindowsSearch.ps1 script is seen here. <\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlockScreenedHead\"><strong><span style=\"font-size: small\"><span style=\"font-size: small\">SearchSpecificWindowsSearch.ps1<\/p>\n<p><\/span><\/span><\/strong><\/p>\n<p class=\"CodeBlockScreened\"><span style=\"font-size: small\"><span style=\"background-color: #f2f2f2\"><span style=\"font-size: small\">$query = &#8220;SELECT System.ItemName, system.ItemPathDisplay, System.ItemTypeText, <br \/><span>&nbsp; <\/span>System.Size FROM SystemIndex where system.size &gt; 10000000000&#8243;<br \/>$objConnection = New-Object -ComObject adodb.connection<br \/>$objrecordset = New-Object -ComObject adodb.recordset<br \/>$objconnection.open(<br \/><span>&nbsp; <\/span>&#8220;Provider=Search.CollatorDSO;Extended Properties=&#8217;Application=Windows&#8217;;&#8221;)<br \/>$objrecordset.open($query, $objConnection)<br \/>Try { $objrecordset.MoveFirst() }<br \/>Catch [system.exception] { &#8220;no records returned&#8221; }<br \/>do <br \/>{<br \/><span>&nbsp;<\/span>Write-host ($objrecordset.Fields.Item(&#8220;System.ItemName&#8221;)).value `<br \/><span>&nbsp;<\/span>($objrecordset.Fields.Item(&#8220;System.ItemPathDisplay&#8221;)).value `<br \/><span>&nbsp;<\/span>($objrecordset.Fields.Item(&#8220;System.ITemTypeText&#8221;)).value `<br \/><span>&nbsp;<\/span>($objrecordset.Fields.Item(&#8220;System.Size&#8221;)).value<br \/><span>&nbsp;<\/span>if(-not($objrecordset.EOF)) {$objrecordset.MoveNext()}<br \/>} Until ($objrecordset.EOF)<\/p>\n<p>$objrecordset.Close()<br \/>$objConnection.Close()<br \/>$objrecordset = $null<br \/>$objConnection = $null<br \/>[gc]::collect()<\/p>\n<p><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: small\"><br \/>There are two things you need to know about querying the Windows Search index. The first is the query syntax, and the second is the provider name. Beyond those two specifics, the script works just like any other script that queries ADO. <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 12pt\"><span style=\"font-size: small\">The query string seen here returns the name, path, type, and size of files that meet specific size criteria. A special form of SQL is used to query the search index that is called Windows Search SQL. It is <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff684395(v=VS.85).aspx\"><span style=\"font-size: small\">documented on MSDN<\/span><\/a><span style=\"font-size: small\">. The field names are documented as part of the shell schema. On MSDN, these <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd561977(v=VS.85).aspx\"><span style=\"font-size: small\">shell schema properties<\/span><\/a><span style=\"font-size: small\"> are grouped into categories. The <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff518152(v=VS.85).aspx\"><span style=\"font-size: small\">system properties<\/span><\/a><span style=\"font-size: small\"> apply to multiple types of files and include properties such as the ones we selected in our script. You do not need to put quotation marks around the property names. Only system properties that are marked <b>isQueryable = true<\/b> in the schema can be used in a query. This information is documented for each property on MSDN. The query syntax is powerful, and <\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb231256(v=VS.85).aspx\"><span style=\"font-size: small\">each portion of the query is documented<\/span><\/a><span style=\"font-size: small\"> on MSDN. <\/p>\n<p><\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span style=\"font-size: small\"><span style=\"background-color: #f2f2f2\"><span style=\"font-size: small\"><br \/>$query = &#8220;SELECT System.ItemName, system.ItemPathDisplay, System.ItemTypeText, <br \/><span>&nbsp; <\/span>System.Size FROM SystemIndex where system.size &gt; 10000000000&#8243;<\/p>\n<p><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: small\"><br \/>The <b>Write-Host <\/b>cmdlet is used to display the value of the properties that are selected. A single command is continued to multiple lines by using the backtick (line continuation) operator. It is the value of the property that is of interest. The <b>Item<\/b> method is used to retrieve a specific property from the <b>recordset<\/b> object. After the property is returned, the value of the property can be retrieved. This is seen here (note that only properties that are selected in the <b>select<\/b> statement can be retrieved from the <b>recordset):<\/b> <\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span style=\"font-size: small\"><span style=\"background-color: #f2f2f2\"><span style=\"font-size: small\">Write-host ($objrecordset.Fields.Item(&#8220;System.ItemName&#8221;)).value `<br \/><span>&nbsp;<\/span>($objrecordset.Fields.Item(&#8220;System.ItemPathDisplay&#8221;)).value `<br \/><span>&nbsp;<\/span>($objrecordset.Fields.Item(&#8220;System.ITemTypeText&#8221;)).value `<br \/><span>&nbsp;<\/span>($objrecordset.Fields.Item(&#8220;System.Size&#8221;)).value<\/p>\n<p><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 12pt\"><span style=\"font-size: small\"><br \/>The rest of the script is basic ADO. Two Hey, Scripting Guy! Blog posts might be of interest to you in helping you use ADO with Windows PowerShell. The first talks about <\/span><a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2006\/10\/02\/how-can-i-use-windows-powershell-to-pull-records-from-a-microsoft-access-database.aspx\"><span style=\"font-size: small\">using Windows PowerShell to query an Access database<\/span><\/a><span style=\"font-size: small\">, and the second article, also talks about <\/span><a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/08\/13\/hey-scripting-guy-can-i-query-a-microsoft-access-database-with-a-windows-powershell-script.aspx\"><span><span style=\"font-size: small\">querying Access<\/span><\/span><\/a><span style=\"font-size: small\">, but features scripts that were submitted for the <\/span><a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/tags\/2009+Summer+Scripting+Games\/default.aspx\"><span style=\"font-size: small\">2009 Summer Scripting Games<\/span><\/a><span style=\"font-size: small\">. Both articles are worth your time. <\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: small\">When the SearchSpecificWindowsSearch.ps1 script is run, the output appears that is shown in the following image. <\/p>\n<p><\/span><\/p>\n<p class=\"Fig-Graphic\"><span style=\"font-size: x-small\"><span style=\"font-size: small\"><img decoding=\"async\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/may\/hey0530\/wes-05-30-10-01.jpg\" alt=\"Image of script output\" style=\"max-width: 600px;border: 0px\"><\/span><\/span><\/p>\n<p class=\"Fig-Graphic\"><span style=\"font-size: 12pt\"><span style=\"font-size: small\">&nbsp;<\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 12pt\"><span style=\"font-size: small\">If you want to know exactly what we will be looking at tomorrow, follow us on <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\"><span style=\"font-size: small\">Twitter<\/span><\/a><span style=\"font-size: small\"> or <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"font-size: small\">Facebook<\/span><\/a><span style=\"font-size: small\">. If you have any questions, send e-mail to us at <\/span><a target=\"_blank\" href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><span style=\"font-size: small\">scripter@microsoft.com<\/span><\/a><span style=\"font-size: small\"> or post your questions on the <\/span><a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\"><span style=\"font-size: small\">Official Scripting Guys Forum<\/span><\/a><span style=\"font-size: small\">. See you tomorrow. Until then, peace.<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'Segoe UI','sans-serif';font-size: 12pt\"><span style=\"font-size: small\">&nbsp;<\/span><\/p>\n<p><\/span>\n<b><span style=\"font-family: 'Segoe UI','sans-serif'\"><span style=\"font-size: small\"><span style=\"font-size: small\">Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/span><\/span><\/b><\/p>\n<p><b><span style=\"font-family: 'Segoe UI','sans-serif'\"><span style=\"font-size: small\"><\/span><\/span><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. One of the cool things about using Windows PowerShell is the different objects with which you can interact. Windows Search is an example of this. By using ADO, you can query the Windows Search index and retrieve information about files. This technique can have some very interesting possibilities. [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[19,146,31,3,147,61,45],"class_list":["post-18211","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-activex-data-objects-ado","tag-databases","tag-operating-system","tag-scripting-guy","tag-search","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. One of the cool things about using Windows PowerShell is the different objects with which you can interact. Windows Search is an example of this. By using ADO, you can query the Windows Search index and retrieve information about files. This technique can have some very interesting possibilities. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/18211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=18211"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/18211\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=18211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=18211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=18211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}