Get the Windows Search Index Status with PowerShell

Image Description

Daily PowerShell #14

Scripting Daily PowerShell Windows Search Index DotNet

October 30, 2021

The Windows Search Index is accessible via the ISearchManager COM interface. Unfortunately, it’s a bit tricky to call it directly in PowerShell.

Generating a .NET Type for the COM Interface

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.

Creating a Cmdlet Library

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 }

Getting the Search Indexer Status

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

Next Steps

It’s possible to extend this library to include all the features of ISearchManager.