How to query and add (if not listed) a location to Windows 10 Search Index using PowerShell

  add, indexing, location, search, windows

I have to re-mount removable drives (which require authentication) each time I boot the computer and Windows Indexing keeps removing the removable drives (perhaps because the removable drives are not available when the computer boots). In an ideal world Windows Indexing would keep these locations and just list them as ‘Unavailable’ (which it sometimes does). However because it doesn’t I am interested in executing a script that queries the Windows Indexing locations and if it does not list the removable drives then add them. At the bottom of this thread I pasted the Batch script that I setup to run at boot (via Start Up folder) to search for a specific folder that is available thereafter mounting one of the removable drives.

I have found several examples of how to do this on Windows 7 (links pasted below) but I can’t figure out how to do it in Windows 10. The links provided to the DLL (Microsoft.Search.Interop.dll) no longer resolve.

When searching for the latest Windows Search SDK for Windows 10 I was lead to the Windows SDK here:
https://docs.microsoft.com/en-us/windows/win32/search/-search-developers-guide-entry-page
I installed the C++ related portion of the Windows SDK then searched for Microsoft.Search.Interop.dll but I couldn’t find it. Perhaps the DLL has changed?

From How to rebuild Windows Search Index by using PowerShell?

Load DLL containing classes & interfaces

Add-Type -path "C:TempSearchIndexSdkMicrosoft.Search.Interop.dll"

#Provides methods for controlling the Search service. This
interface manages settings and objects that affect the search engine
across catalogs.
https://msdn.microsoft.com/en-us/library/bb231485(v=vs.85).aspx

$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass

#Retrieves a catalog by name and creates a new ISearchCatalogManager
object for that catalog.

$catalog = $sm.GetCatalog("SystemIndex")

#Resets the underlying catalog by rebuilding the databases and performing a full indexing.

#https://msdn.microsoft.com/en-us/library/bb266414(v=vs.85).aspx

$catalog.Reset()

From How to add a location to windows 7/8 search index using batch or vbscript?

#Code copied from "Powershell Tackles Windows Desktop Search" http://powertoe.wordpress.com/2010/05/17/powershell-tackles-windows-desktop-search/
#Microsoft.Search.Interop.dll is needed, download from http://www.microsoft.com/en-us/download/details.aspx?id=7388

#Load the dll

Add-Type -path "D:UnattendUserFilesToolsMicrosoft.Search.Interop.dll"

#Create an instance of CSearchManagerClass

$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass

#Next we connect to the SystemIndex catalog

$catalog = $sm.GetCatalog("SystemIndex")

#Get the interface to the scope rule manager

$crawlman = $catalog.GetCrawlScopeManager()

#add scope

$crawlman.AddUserScopeRule("file:///D:*",$true,$false,$null)

$crawlman.SaveAll()

I would add a comment to the existing threads but I am not able to because I don’t have reputation of 50 (dumb rule IMO).

Last… I found this site which lists the DLL along with some code but it hasn’t been updated in a long time.

https://github.com/FileMeta/WindowsSearchSample

Thanks in advance!

Batch script that runs at boot:

@echo off

echo Windows Search is being restarted to recognize the Z drive

:while

if EXIST Z:Watch (

I WANT TO CALL POWERSHELL SCRIPT TO ADD THE LOCATION TO THE INDEX IF NEEDED HERE

sc stop WMPNetworkSvc

ping 127.0.0.1 -n 5 > nul

sc stop WSearch

ping 127.0.0.1 -n 5 > nul

sc start WSearch

ping 127.0.0.1 -n 5 > nul

sc start WMPNetworkSvc

echo Exiting this script in 5 seconds

ping 127.0.0.1 -n 5 > nul

exit

) else (

echo Waiting 60 seconds to check if Z drive is available

ping 127.0.0.1 -n 60 > nul

goto :while

)

Source: Windows Questions

LEAVE A COMMENT