- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
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.
Get-ChildItem -Path C:\ | Where-Object {$_.Name}
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.
function Get-ListOfFiles { [CmdletBinding()] param( [string]$Path ) Get-ChildItem -Path $Path }
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.
PS C:\> Get-ChildItem -Path C:\Windows -File | Get-Member
It looks like we have a System.IO.FileInfo object. Knowing this, I'll add this to the OutputType keyword in our function.
function Get-ListOfFiles { [OutputType('System.IO.FileInfo')] [CmdletBinding()] param( [string]$Path ) Get-ChildItem -File -Path $Path }
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!
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.
Subscribe to 4sysops newsletter!
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.