6

Is there a way to ignore Windows search indexing for certain patterns?

For example, I would like to skip the following folders:

C:\Git**.git\ C:\Git**\packages\ C:\misc\git**.git\ C:\misc\git**\packages\

These cause thousands of extra noise matches in search results.

I do want to index the various folders in C:\misc\git except for what's in the .git or .packages subfolders. I've only found a static way to do this, and nothing based on the pattern. It seems like this would be a common use case and is a little bit like the way git uses .gitignore to keep certain content out of version control. In this case, it would be keeping it out of the search index.

I do not see any facility for patterns like this in "Indexed Locations". I assume it could be done by running some kind of script to exclude the current matches each time content changed (e.g. C:\misc\git\somethingnew was added) but that would be the opposite of elegant.

Any help would be appreciated.

Penny

Penny
  • 347
  • 4
  • 12
  • This is pretty useful as well https://superuser.com/questions/235799/exclude-directories-from-windows-search-by-wildcard – bordeaux Jul 10 '23 at 17:59

2 Answers2

4

According to this answer by @PryrtCJ Windows registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Search\CrawlScopeManager\Windows\SystemIndex\WorkingSetRules\ contains rules, which allow to use wildcards.

regedit with *.git\ exclude rule

To add such rules I suggest to add one .git folder in indexing options to excluded paths and then find proper key (URL would be sth. like file:///C:\[partition-or-disk-id]\path\to\.git\). Then edit key permissions (select key containing the .git URL and right-click it) - you'll need to change the key owner from SYSTEM to your account and allow Administrators group to modify key.

advanced permissions settings

If you have permissions to modify it, right-click URL, select modify and change it's content to file:///*\.git\. Later restore previous permissions (just to avoid problems).

This way you can create any * filters so eg. file:///C:\misc\*\packages\*

3

The Windows Search Index can be managed programmatically.

You can exclude folders from the Search Index using the following patterns :

  • file:///C:\*\packages\ matches a folder named "packages" and its content
  • file:///D:\*\obj\ matches a folder named "obj" and its content

I tested these patterns successfully using C# under Windows 11 21h2.

Example to add a new rule (Powershell)

# [wsearch-add-rule.ps1]
# Source: https://stackoverflow.com/questions/13390514/how-to-add-a-location-to-windows-7-8-search-index-using-batch-or-vbscript/13454571#13454571
Add-Type -path ".\Microsoft.Search.Interop.dll"
$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass 
$cat = $sm.GetCatalog("SystemIndex")
$csm = $cat.GetCrawlScopeManager()
$csm.AddUserScopeRule("file:///D:\*\obj\", $true, $false, $null)
$csm.SaveAll()

Example to list existing rules (Powershell)

# [wsearch-list-rules.ps1]
# Source: https://powertoe.wordpress.com/2010/05/17/powershell-tackles-windows-desktop-search/
Add-Type -path "Microsoft.Search.Interop.dll"
$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass 
$cat = $sm.GetCatalog("SystemIndex")
$csm = $cat.GetCrawlScopeManager()
$scopes = @()
$begin = $true
[Microsoft.Search.Interop.CSearchScopeRule]$scope = $null
$enum = $csm.EnumerateScopeRules() 
while ($scope -ne $null -or $begin) {
     $enum.Next(1,[ref]$scope,[ref]$null)
     $begin = $false
     $scopes += $scope
}
$scopes|ogv

The required Microsoft.Search.Interop.dll can be found in the old Windows Search 3x SDK on web archive (the url must be copied and pasted in a new tab)

Resources :

JBress
  • 121
  • 1
  • 7
  • Is the only way to manage it by installing an external dll? – gargoylebident Apr 13 '22 at 04:10
  • Microsoft.Search.Interop.dll is required when the index is handled via .NET (managed project or Powershell). Note that the dll can also be generated on the fly. More info [here](https://stackoverflow.com/questions/6979545/c-sharp-microsoft-search-interop-cannot-be-found/) – JBress Apr 13 '22 at 06:27