Ironman Software Forums
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
The Windows Search Index is accessible via the ISearchManager COM interface. Unfortunately, it’s a bit tricky to call it directly in PowerShell.
You can use the tlbimp
tool that is included with Visual Studio and the Windows SDK to compile the searchapi.tlb
file into a .NET class library. This can then be used in PowerShell. You will need the Windows SDK installed.
tlbimp.exe "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x64\searchapi.tlb" /namespace:Microsoft.Search.Interop /out:Microsoft.Search.Interop.dll
This has been compiled and is available in our GitHub repository.
The DLL generated by tlbimp
does not work directly in PowerShell. It is possible to access some of the types and methods but not all of them. To work around this, I developed a simple cmdlet for calling the ISearchManager
class.
The cmdlet gets the default catalog and returns the status of the indexer.
[Cmdlet(VerbsCommon.Get, "SearchIndexStatus")]
public class GetSearchIndexStatusCommand : PSCmdlet
{
protected override void ProcessRecord()
{
CSearchManager manager = new CSearchManager();
// SystemIndex catalog is the default catalog in Windows
ISearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex");
catalogManager.GetCatalogStatus(out _CatalogStatus status, out _CatalogPausedReason reason);
var numberOfItems = catalogManager.NumberOfItems();
var urlBeingIndex = catalogManager.URLBeingIndexed();
WriteObject(new Hashtable()
{
{ "Status", status },
{ "Reason", reason },
{ "NumberOfItems", numberOfItems },
{ "URLBeingIndexed", urlBeingIndex }
});
}
}
Now that this has been compiled, you can use the library by import the assemblies and calling Get-SearchIndexStatus
PS> ipmo .\Microsoft.Search.Interop.dll
PS> ipmo .\searchindex.dll
PS> Get-SearchIndexStatus
Name Value
---- -----
NumberOfItems 74576
URLBeingIndexed file:C:/Users/adamr/Dropbox/.dropbox.cache/marker_file
Reason CATALOG_PAUSED_REASON_EXTERNAL
Status CATALOG_STATUS_PAUSED
It’s possible to extend this library to include all the features of ISearchManager
.
Find this useful? Please consider sharing this article. Have a question about PowerShell? Contact us and we'll write a post about it.
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
Receive once-a-month updates about Ironman Software. You'll learn about our product updates and blogs related to PowerShell.