Output 5 properties as a table?

This topic contains 6 replies, has 2 voices, and was last updated by  js 3 weeks, 2 days ago.

  • Author
    Posts
  • #101431

    js
    Participant

    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
    
    
    
    
    
  • #101442

    Don Jones
    Keymaster

    Nope. Use format-table.

    • #101460

      js
      Participant

      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'"

  • #101461

    Don Jones
    Keymaster

    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.

    • #101463

      js
      Participant

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

  • #101464

    Don Jones
    Keymaster

    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.

    • #101467

      js
      Participant

      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
      

You must be logged in to reply to this topic.