Formatting AD Objects

Welcome Forums General PowerShell Q&A Formatting AD Objects

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
6 months, 3 weeks ago.

  • Author
    Posts
  • #99292

    Participant
    Points: 4
    Rank: Member

    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?

  • #99445

    Participant
    Points: 56
    Rank: Member

    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/

  • #99450

    Participant
    Points: 0
    Rank: Member

    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)

  • #99538

    Participant
    Points: 4
    Rank: Member

    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.

The topic ‘Formatting AD Objects’ is closed to new replies.