Author Posts

April 19, 2018 at 10:01 pm

I am finally getting around to creating formatting (ps1xml) for the output of my functions. I have one function that returns objects of type Microsoft.ActiveDirectory.Management.ADGroup. I would like to have the default output for this function to still be an ADGroup object (so I don't want to change the TypeName). But I want to format the output for this function differently than the default (format-table instead of format-list).

I know that formatting in the script with a format-table makes the output not be an ADGroup any more (and is bad practice anyway). I could run the function and pipe it into format-table to get what I want, but would like to have a different default output format so I don't have to remember to do that each time.

Ideas?

April 22, 2018 at 11:17 pm

Take a look at the results from Iron script prequel 2 – great way to look at formatting your code just the way you want.
https://powershell.org/2018/01/28/iron-scripter-prequel-puzzle-2-a-commentary/

April 23, 2018 at 4:43 am

Hey Darwin,
It sounds like you have a slight miss-understanding of the output. Powershell will, by default, change between using Format-Table or Format-List for its output based on the number of properties passed for the object being displayed.
4 or less properties and it will default to Format-Table. 5 or more properties and it will default to Format-List (Ref: https://workingsysadmin.com/powershell-rules-for-format-table-and-format-list/)

So there are a few options available to you.
1) You can use Select-Object and select 4 or less properties you actually want output and it will default to Format-Table
2) You can Pipe the output to the formatter you wish to use each time so it is formatted as you desire. IE. | Format-Table
3) You can change the default behavior and write your own output handling in the Out-Default cmdlet. (Ref: https://ss64.com/ps/out-default.html)

April 23, 2018 at 7:06 pm

Aha! Select-Object does not change the object type, so it remains an ADGroup object type. I didn't realize that. This is exactly what I needed. Thanks.