powershell equivalent to get-argumentcompleter

Viewed 353

Register-ArgumentCompleter is an incredibly useful function but I have a suspicion that overzealous use of it by some modules has caused my shell to slow down. I would like to see a list of all the ArgumentCompleters that are Registered in my Session.

I had an idea for how to write one, but it is overly complex and I have my doubts it would work, so if anyone knows of a solution already I would greatly appreciate it.

2 Answers

Have a look at the module ImpliedReflection

You can then use this line to read the argument completers. To access with without the ImpliedReflection module, use standard .net reflection.

$ExecutionContext._invokeCommand._context.CustomArgumentCompleters.Keys

I don't think this is possible from looking at Register-ArgumentCompleter's definition in the PowerShell 6 code.

It seems that the actual dictionaries with the ScriptBlocks mapped to "command:parameter" keys are in Context.CustomArgumentCompleters which is an ExecutionContext (internal class) inherited from InternalCommand. I don't see any interface to enumerate the ArgumentCompleter dictionaries from outside of System.Management.Automation.

Thus @BenH's suggestion to use TabExpansionPlusPlus as a workaround might help diagnose as it appears to replace the builtin Register-ArgumentCompleter and TabExpansion2 with a custom one that can be inspected (so if you load this module early enough you can look at modules that might be registering a lot of completer). However if something is invoking System.Management.Automation.RegisterArgumentCompleterCommand directly you might need to attach a debugger.

Related