Author Posts

May 30, 2018 at 1:14 pm

Is there some variable I can set to 5 instead of 4, so that 5 properties in a script will output as a table instead of a list by default?

PS /Users/js> [pscustomobject]@{name='me';address='here';city='la';state='ca';zip=11111
  PSTypeName = 'MyObject'}   



name    : me
address : here
city    : la
state   : ca
zip     : 11111




May 30, 2018 at 1:24 pm

Nope. Use format-table.

May 30, 2018 at 8:22 pm

How does get-process do it? Can I have a script output a new object, and then use a simple here-string or file with update-formatdata that makes it use format-table by default? I guess [OutputType('MyObject')] doesn't work in scripts.

EDIT:
Ok, I have "PSTypeName = 'MyObject'"

May 30, 2018 at 9:54 pm

Get-Process has a predefined view in a .format.ps1xml file; that mandates that the output object have a unique type name (which you can apply). Covered extensively in “PowerShell In Depth” if you're interested ;). But yes, you apply a custom type name to the object, and then import a .format.ps1xml file.

May 30, 2018 at 9:58 pm

How does the format file pick whether the default view is table or list?

May 30, 2018 at 10:01 pm

The format file defines a view, which I s either a table view or a list view. If it defines more than one, then the first one is the one it uses unless you use the -Name parameter of a Format- command to specify a different view. Process objects, for example, have two defined table views and the first one is the one you normally see.

May 31, 2018 at 12:07 am

Ok, here it is. It would be nice if I could use a here-string.

update-formatdata myobject.format.ps1xml
[pscustomobject]@{name='me';address='here';city='la';state='ca';zip=11111
  PSTypeName = 'MyObject'}

Name             Address          City             State            Zip
----             -------          ----             -----            ---
me               here             la               ca               11111