Get Available Constructors Using PowerShell

Working with PowerShell sometimes (or a lot of the time) involves using .Net types to create objects that can be used for more in depth scripting of various items. It is one thing to know the type that you need for the task at hand, but another all together to use that type and build  an object that can be used for whatever purpose. Sometimes it is as simple as just doing the following:

New-Object Net.Sockets.TCPClient


But did you know that there are other parameters that can be used with this type? You can actually specify a system name and port number as an argument to the object creation that will actually perform a port query.

New-Object Net.Sockets.TCPClient -ArgumentList,389


You can see that not only did it create the TCPClient object, but it already attempted a port connection to the server. Pretty cool stuff!

So how do you figure out what to use when creating an object? Well, you can either go out to the MSDN site and search for that specific type and look at the various constructors available, or you can dig a little in the .Net object and find out yourself!

([type]"Net.Sockets.TCPClient").GetConstructors() | ForEach {
} | Select Name,ParameterType


Ok, so it still isn’t that clear at to what parameters are required for each available constructor for this type. But at least you now know more than before. Lets take it another step forward and actually see what each constructor has available.

([type]"Net.Sockets.TCPClient").GetConstructors() | ForEach {
    ($_.GetParameters() | ForEach {$_.ToString()}) -Join ", "


That’s a little better. At least we can now more easily see the parameters and respective parameter types that are required for the constructor. The initial blank line means that this constructor also accepts no parameters to create the object.

To make this a little easier, I use a function I wrote called… Get-Constructor. Using this, I am able to quickly get all available constructors either written out as a simple string, or as an object if I wish to dive deeper into the .Net object. The download for this function is available below in the Script Repository.

A couple examples of Get-Constructor in action:

Display as a simple string

Get-Constructor -Type Net.Sockets.TCPClient


As an object

Get-Constructor -Type Net.Sockets.TCPClient -AsObject


A couple more just for fun

Get-Constructor -Type string



Get-Constructor -Type adsisearcher


That is all there is to it! Feel free to give it a download and let me know what you think!

Download Get-Constructor

Script Repository

This entry was posted in powershell, scripts and tagged , , . Bookmark the permalink.

4 Responses to Get Available Constructors Using PowerShell

  1. Allan says:

    Joel, your Get-Constructor code on poshcode is brilliant! I borrowed it a few years ago when putting together notes about how to use DLLs in Powershell. My notes are published here and do reference a poshcode link, although not 3993.

  2. pamkkkkk says:

    One of the best and collest PowerShell vs. .NET Tips!
    Thank you for that Boe!
    Peter Kriegel

  3. my reflection module (available on has a get-constructor which even includes the PowerShell syntax for invoking the constructors (including how to cast arrays as parameters, etc.), you should check it out 😉

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s