Auto-Complete .NET Constructor Params


If you’re a regular PowerShell script or module author, you’re probably accustomed to creating .NET objects using object constructors. In the Microsoft .NET Framework, each struct or class can have one or more constructors. A constructor enables the type consumer to instantiate the object using a set of zero or more input parameters, as declared by the library author.

For example, if you are building a Person class, with two properties, FirstName and LastName, you might want to ensure that a Person object cannot be instantiated with a null FirstName or LastName parameter. To achieve this, you might implement a constructor that accepts two parameters: First and Last. In the body of the constructor, you would perform validation, to ensure a non-null, and non-empty string value, and then assign the value of the input parameters to the corresponding properties on the class instance. Consequently, when a library consumer instantiates the Person class, it would be impossible for them to create a Person with a null or empty FirstName and LastName property.

In the Windows PowerShell environment, instantiating .NET objects is a common task for script and module authors. In many cases, where a native module does not exist, a module author will be required to wrap a PowerShell module around an existing .NET library. This scenario generally requires direct interaction with .NET types, commonly requiring instantiation of .NET classes.

Unfortunately, the PowerShell automation library (System.Management.Automation.dll) does not expose Intellisense / auto-completion capabilities for .NET objects. The net result of this missing capability is that the PowerShell console host, Integrated Scripting Editor (ISE) host, and even the Visual Studio PowerShell Tools (PoshTools) host, do not offer auto-completion (aka. tab-completion) for .NET type constructors. Therefore, the PowerShell developer would be responsible for determining the valid constructor parameters, for each type, via documentation (which is commonly unreliable) or through an Intermediate Language (IL) disassembler tool, such as RedGate .NET Reflector, ILSpy, the Visual Studio Object Browser, and related tools. Depending on certain variables, such as the availability of documentation, and the rapid accessibility of IL disassembler tools, this discovery process can take some time. If this process is repeated for multiple object types, the time spent researching is significantly increased, thereby reducing the productivity of the PowerShell developer.

NOTE: Back in 2012, I filed this feedback on Microsoft Connect, and as of this writing, it has 25 upvotes, and zero downvotes. It is apparent that quite a few people want this functionality in the native product. If you have not already up-voted this feedback, please up-vote it and leave a comment to the PowerShell team, letting them know that you would like to see support for this productivity enhancement. In the meantime, my solution should provide an adequate work-around.


As an alternative to this manual research effort, by the PowerShell developer, I have written a productivity enhancement tool. This tool enables the PowerShell developer to obtain instant auto-completion / Intellisense for .NET object constructors. The developer will immediately be able to understand the various constructor signatures that are available, including the availability of default object constructors, which do not accept any parameters. Not all object types expose default constructors, depending on the intent of the .NET library author.

The tool I have written is a simple auto-completer function for the TabExpansion++ PowerShell module that was created by Jason Shirk, an employee of Microsoft, on the PowerShell core team. When the New-Object command is added to a script, with the -TypeName parameter, and the -ArgumentList parameter is added, the auto-completer function will automatically determine the available constructor signatures, and prompt the developer to select one, reducing the amount of research and typing that is required by the developer.

New-Object ArgumentList Auto-Completer


In order to utilize the auto-completer function for the New-Object command’s -ArgumentList parameter, follow the steps below.

  1. Copy-install the TabExpansion++ PowerShell module by Jason Shirk.
    1. NOTE: It’s important to download the ZIP file for the DropScanningRegistration Git branch. Do not download the master branch. Use the above link, for the time being.
  2. Copy the custom argument completer into the TabExpansion++ folder.
  3. Start a new PowerShell session (console host, ISE, or Visual Studio PowerShell Tools)
  4. Import the TabExpansion++ PowerShell module (eg. Import-Module -Name TabExpansion++), or optionally add the import command to your PowerShell profile script.
    1. NOTE: The custom argument completer should automatically be imported with the module. Make sure that the .ArgumentCompleters.ps1 suffix is maintained, otherwise it will not automatically be picked up by the import process.
  5. Create a new, empty PowerShell script, and add the New-Object -TypeName System.Data.SqlClient.SqlConnection command.
  6. Add the -ArgumentList parameter, put a space after it, and notice the Intellisense pop up with the .NET object constructor signatures.

After following the above steps, you should be well on your way to being more productive with .NET objects in PowerShell! I hope this article has been helpful, and increases the pace with which you can author PowerShell automation scripts and modules.

Getting Started Video

If you’d like to watch a video about how to install TabExpansion++ and the custom argument completer, watch the video that I produced below.