PowerShell Output types and options

Hi Guys,

I’ve just spent ages trying to find my own posts so I could check for any answers to this question, only to discover I hadn’t actually posted this yet. :oops:

So, with standard cmdlets etc e.g. Get-VM you get something like this:

Name               PowerState    Num CPUs    MemoryGB

HOSTNAME    PoweredOn    4                    12.000

but if you run it and pipe to format-list e.g. Get-VM | fl then you get something like this:

Name                                                                         : HOSTNAME
PowerState                                                                   : PoweredOn
NumCpu                                                                       : 4

                           "{0:N3}" -f $_.MemoryGB
                         : 12.000
HardDisks                                                                    : {Hard disk 1, Hard disk 2, Hard disk 3}
CDDrives                                                                     : {CD/DVD drive 1}
FloppyDrives                                                                 : {}
NetworkAdapters                                                              : {Network adapter 1}
UsbDevices                                                                   : {}
Host                                                                         : hostname.example.com
Id                                                                           : VirtualMachine-vm-26
Notes                                                                        : vCenter Server 5.5

So I’ve written (hacked) a script to get information about vm resets or snapshot creation etc. and I return an object to allow me to effectively use the pipeline, but I can’t see how to emulate the behaviour above. Either I format the output and I get a fixed output or I output the object and all properties are returned by default as a list.

What I’d like is to have a full list of properties like :

UserName,Key,Task,Description,Name,DescriptionId,Entity,EntityName,Locked,State,Cancelled,Cancelable,Error,Result,Progress,Reason,QueueTime,StartTime,CompleteTime,EventChainId,ChangeTag,ParentTaskKey,RootTaskKey,LinkedView,DynamicType,DynamicProperty

Choose default output properties e.g. UserName,EntityName,Description,CompleteTime which I think will default as a table output or the option of piping the output to format-list or select-object and get more properties from the object.

Does that all make sense and can someone tell me how to do this? I feel like this should be a standard option and perhaps I just haven’t found the syntax yet. I’ve watched the MVA Basic Level PowerShell 3.0 JumpStart and have completed at least half of the Advanced Tools & Scripting, but haven’t yet seen this yet.

The default output displayed by PowerShell can be determined by a type extension or formatting file, meaning that the default output can be “trimmed down” to whatever the developer thought was “most interesting.” Piping to Select * or Format-List * overrides that behavior and shows everything. The object output by the command is always complete; it’s just the display-to-screen process where it gets any default view applied.

If you output a custom object, then the shell won’t have any defaults, so it’ll display everything by default. Whether it uses a list or table depends on how many properties there are - 5 or more, you get a list by default.

If you want to modify the default display for an existing object type, or for a custom object type, then you have to get into defining a default custom view in a .ps1xml file. Learn PowerShell Toolmaking in a Month of Lunches covers doing so, but it’s definitely an advanced topic that has a lot of pre-requisites. I’d suggest grabbing Learn PowerShell in a Month of Lunches to start with. The first 5-6 chapters are essentially working you through those core concepts.

Nice plug :wink: I’d certainly like to take a look at that book. I’ll have a look in my local library :stuck_out_tongue:

On a more serious note, thanks for the tip, which gave me enough explanation to understand what’s happening and why I wasn’t getting what I wanted. Hopefully I can find a deal to get both your books as they seem to come highly recommended and would probably help me to understand PowerShell better and use it to the MAX!

Odd that Amazon suggests buying the first edition of “Learn Windows PowerShell in a Month of Lunches” along with “Learn PowerShell Toolmaking in a Month of Lunches” and the second edition of “Learn Windows PowerShell in a Month of Lunches”.

Unless you’re a serious book collector, is there any reason to buy a copy of the first edition?

Bill,

I don’t have access to VMWare environment, but in Hyper-V 2012 R2, Get-VM returns a different set of properties.

From the notes you posted, I see the VMWare Get-VM cmdlet returns a complex object. That’s an object, that has other objects as one or more of its properties. To get the properties you want, you can create a PS Custom object and pick the properties needed from the original object or its sub-objects. To help you further, I need Get-Member cmdlet output of the objects that have the properties you’re after. For example output of

Get-VM | GM