Powershell: Intellisense for `New-Object`'s `ComObject` parameterset

Created on 22 Aug 2019  路  3Comments  路  Source: PowerShell/PowerShell

Summary of the new feature/enhancement

Following the release of v7.preview3, there was a blog post showcasing the support of COM objects displaying their method argument names now.

https://devblogs.microsoft.com/powershell/powershell-7-preview-3/

Going further, it would be fantastic if the New-Object cmdlet had intellisense for its COM parameter set like what exists for the TypeName parameter set instead of completing from the PWD. It is a common scenario that I have to run registry queries to see which COM classes are available.

Proposed technical implementation details (optional)

New-Object -ComObject Shell.App<TAB>

Also <C-SPACE> for all options.

Issue-Enhancement Up-for-Grabs

Most helpful comment

TabExpansionPlusPlus also implements this completion, see this file. Note that many of the other completers in that file should be added to PowerShell.

It's also worth mentioning that having a generic caching mechanism for completions like you'd want for -ComObject would be a valuable addition.

All 3 comments

As a user-side / workaround, it's possible to add them with Register-ArgumentCompleter as in this profile script; line 13 collects and stores them in a cache file, line 74 adds completion for New-Object -ComObject parameter.

Posting that here shortened as a form of documentation:

if (-not (Test-Path $PSScriptRoot\Cache\CompletionCache.ps1xml)) {
    [void](New-Item -Path $PSScriptRoot\Cache -ItemType Directory)

    Get-CimInstance -ClassName Win32_ClassicCOMClassSetting -Filter 'VersionIndependentProgId IS NOT NULL' |
        Select-Object -Property @(
            @{L='ProgId'; E={ $_.VersionIndependentProgId }}
            @{L='Caption'; E={ ($_.VersionIndependentProgId, $_.Caption)[!!$_.Caption] }}
        ) | Export-CliXml $PSScriptRoot\Cache\CompletionCache.ps1xml
}

$script:completionCache = Import-CliXml $PSScriptRoot\Cache\CompletionCache.ps1xml

Register-ArgumentCompleter -CommandName New-Object -ParameterName ComObject -ScriptBlock {
    param(
        [string]
        $CommandName,

        [string]
        $ParameterName,

        [string]
        $WordToComplete,

        [System.Management.Automation.Language.CommandAst]
        $CommandAst,

        [System.Collections.IDictionary]
        $FakeBoundParameters
    )

    $script:completionCache.Where{ $_.ProgId -like "$WordToComplete*" }.ForEach{
        [Management.Automation.CompletionResult]::new($_.ProgId, $_.ProgId, 'Type', $_.Caption)
    }
}

TabExpansionPlusPlus also implements this completion, see this file. Note that many of the other completers in that file should be added to PowerShell.

It's also worth mentioning that having a generic caching mechanism for completions like you'd want for -ComObject would be a valuable addition.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abock picture abock  路  3Comments

JohnLBevan picture JohnLBevan  路  3Comments

andschwa picture andschwa  路  3Comments

lzybkr picture lzybkr  路  3Comments

manofspirit picture manofspirit  路  3Comments