PowerShell's tab-completion ability is great, but by using the OutputType keyword properly in your PowerShell functions, we can make it even better.

Adam Bertram

Adam is a Microsoft Cloud and Datacenter Management Most Valuable Profressional (MVP) who specializes in Windows PowerShell. You can reach Adam at adamtheautomator.com or on Twitter at @adbertram.

Whenever you're working with PowerShell, have you ever noticed that at certain times, you could start hitting the Tab key, and it somehow knows what's available to you? This tab completion can work with function parameters, variables, and more because PowerShell knows what's available a lot of the time. Also, when using the built-in cmdlets like Get-ChildItem and building a Where-Object filter, PowerShell knows what properties you can filter on.

Filtering with the Where Object

Filtering with the Where Object

Notice in the above example I'm filtering on the Name property. To find the property name, I didn't have to know it. I just needed to type $_. and start hitting the Tab key. PowerShell then happily goes through all the properties I can use. How does it know that anyway? The answer is with the OutputType keyword.

When used with a function, the OutputType keyword defines what type of object a function will return. If PowerShell knows this ahead of time, it can give you all the property names of that type of object.

For example, let's say I have a function that calls Get-ChildItem.

As is, this is just a wrapper function around Get-ChildItem that passes the Path value directly to Get-ChildItem. Since we're essentially doing the same thing, I wonder if PowerShell will allow me to tab-complete my pipeline variable as it did when we were directly calling Get-ChildItem. The answer is no because we have not told PowerShell what type of object our function is returning. Let's fix that.

We first need to figure out what type of object Get-ChildItem returns.

Determining the object type

Determining the object type

It looks like we have a System.IO.FileInfo object. Knowing this, I'll add this to the OutputType keyword in our function.

I'll type out Get-ListOfFiles -Path C:\ | Where-Object { $_. } and hit the tab key. Notice now that PowerShell is tab-completing the properties just like it did with Get-ChildItem!

Tab completion for your own function

Tab completion for your own function

The OutputType keyword isn't required, by any means, to make a good script or function. As you can see, it does provide the user with a better experience, and if the user looks at the source code, it also acts as documentation to quickly show what kind of object the function returns. However, the function will work just fine without it.

Using the OutputType keyword is putting the cherry on top of a custom script or function. Nine times out of ten, others won't notice it, but when they do, they will appreciate that little extra effort you have put into the details of your code.

Win the monthly 4sysops member prize for IT pros


Users who have LIKED this post:

  • avatar
  • avatar

Related Posts


Leave a reply

Your email address will not be published. Required fields are marked *



Please ask IT administration questions in the forum. Any other messages are welcome.

© 4sysops 2006 - 2017

Log in with your credentials


Forgot your details?

Create Account