What is OutputType?

If you've seen the OutputType attribute when writing a cmdlet or advanced function, you might wonder what we use it for.

The goal of the OutputType attribute is to provide a mechanism for tools to know what your cmdlets may output without running them. If they know that without running it, they can provide useful services – such as tab completion, data flow analysis, etc.

For example:

Get-Process | Where-Object { $_.<TAB>

When you push TAB, we can look at the pipeline that you’ve just typed, do static analysis of the cmdlet that Where-Object is filtering, and then only display properties from the OutputType (if that cmdlets specifies one).

When writing a cmdlet or advanced funciton, you can type whatever you want as the output type, but you’ll mislead tools if it doesn’t represent a real type that can be reasoned against. The reason that we support the string syntax (in addition to the regular type syntax) is so that the type doesn’t need to be loaded in order for the script to run.

2 Responses to “What is OutputType?”

  1. James O'Neill writes:

    Lee have you see bug 746262 on connect – outputtype is supposed to be able to return different types depending on the parameter set, but it ignores the parameter set specified – so the example in about_Functions_OutputTypeAttribute does not work

  2. OutputType(): Optional but Recommended | Adam the Automator writes:

    […] works fine without it. That is until I came across Lee Holmes’ excellent blog post entitled What is OutputType()?. Not only does using OutputType() add tab-completion functionality to your object but the […]

Leave a Reply