none
PSobject Has table to PStables RRS feed

  • Question

  • Hi Guys,

    I having real hard time to figure out why same code used by me in tow different scripts result into two different views of psobject table

    $states = @{"Name" = "$N"; "SourceDatabase" = "$so"; TargetDatabase = "$TG";"Status" = "$ST"}
    $Psobj = new-object -TypeName PSObject -Property $states
    $Psobj

    cause : ( View 1)

    SourceDatabase : XYX-10
    Name           : X.Y
    TargetDatabase : XYZ-10

    Status         : Failed


    SourceDatabase : XYX-10
    Name           : A.B
    TargetDatabase : MTLDAG04-DB4
    Status         : XYZ-10

    and In other script it gives me ( View -2 )

    $hash = @{"Displayname" = "$ds" ; "ItemCount" = "$itc" ; "TotalItemSize" = "$itz" ; "EmailAddress" = "$EL" }
    $Table = New-Object -TypeName Psobject -Property $hash
    $table

    Displayname                   ItemCount                     TotalItemSize                 EmailAddress
    -----------                   ---------                     -------------                 ------------
    A.b                  113                           1.626 MB (1,704,471 bytes)    a.h@xyz.ca
    C.d                  5234                          452.5 MB (474,456,513 bytes)  a.c@xyz.ca
    E.f                  24693                         2.506 GB (2,690,950,345 by... b.v@xyz.ca

    The difference can be seen on same server , same shell session as well

    Thanks in Advance .

    I am looking to get second view consistently

    Saturday, January 4, 2014 6:58 PM

Answers

  • It is just the way the default formatters work. Different structure different output.

    You also don't and should not overquote everything.

    $states=@{ Name =$N SourceDatabase=$so TargetDatabase=$TG Status=$ST } new-object PSObject -Property $states

    This is more readable, understandable and avoids the potential for some errors.

    When I do it like this I get no wrapping.  In your data you may have arrays and other objects that are being expanded by all of the extra quotes.  Remove the quotes and it is more likely you will get a better display.

    It is also impossible to say exactly what is happening because you have failed to post the most important part of you code.

    Look into the default formatters and how type formatting is done in PowerShell.


    ¯\_(ツ)_/¯

    • Marked as answer by Nitinswa Sunday, January 5, 2014 5:02 AM
    Saturday, January 4, 2014 8:22 PM
  • jrv is correct that it's the formatting system at work. Its trying to show you your objects in a way that's readable most of the time. So, it will show an object with less than five properties as a table if you don't tell it how to display it. It will show your object as a list if it has five or more properties. You can force it to a certain view by piping the results to 'Format-List' or 'Format-Table' (be careful, though, because this will make it so you can't usefully pipe those results to anything else; the Format-* cmdlets will make it so your objects are pretty much only good for looking at in the host).

    Now, that's how it works when you're interactively displaying one item at a time. If more than one object is coming out at a time, the formatting system doesn't follow the previous rules (it still follows rules, though). If two objects come out, it will format them both the way that it formatted the first one. This means that if an object that PS would normally format as a list comes out, and an object that it would normally format as a table comes out right after it, they will both be formatted as a list. To see this in action, open an ISE session and run the following lines one at a time (or in the console paste the lines in one at a time):

    # Run these first two commands to define two objects
    $List = New-Object PSObject -Property @{ a = 1; b = 2; c = 3; d = 4; e = 5 }
    $Table = New-Object PSObject -Property @{ a = 1; b = 2; c = 3; d = 4 }
    
    # Run this by itself, it will show as a list b/c there are at least
    # five items:
    $List
    
    # Run this by itself, it will show as a table b/c there are less than
    # five items:
    $Table
    
    # If you run this they will both show as a table b/c the first object 
    # comes out as a table. You will also be missing the 'e' column since 
    # the first object didn't show that
    $Table; $List
    
    # If you run this, they will both show as a list b/c the first object 
    # comes out as a list. All properties will show for each object
    $List; $Table
    

    So, if you're not interested in keeping your objects as true objects that can be worked with later (using ForEach-Object, Where-Object, Export-Csv, etc), then you could do this to force the output as a table:

    $Objects | Format-Table

    • Marked as answer by Nitinswa Sunday, January 5, 2014 5:02 AM
    Saturday, January 4, 2014 8:43 PM

All replies

  • It is just the way the default formatters work. Different structure different output.

    You also don't and should not overquote everything.

    $states=@{ Name =$N SourceDatabase=$so TargetDatabase=$TG Status=$ST } new-object PSObject -Property $states

    This is more readable, understandable and avoids the potential for some errors.

    When I do it like this I get no wrapping.  In your data you may have arrays and other objects that are being expanded by all of the extra quotes.  Remove the quotes and it is more likely you will get a better display.

    It is also impossible to say exactly what is happening because you have failed to post the most important part of you code.

    Look into the default formatters and how type formatting is done in PowerShell.


    ¯\_(ツ)_/¯

    • Marked as answer by Nitinswa Sunday, January 5, 2014 5:02 AM
    Saturday, January 4, 2014 8:22 PM
  • jrv is correct that it's the formatting system at work. Its trying to show you your objects in a way that's readable most of the time. So, it will show an object with less than five properties as a table if you don't tell it how to display it. It will show your object as a list if it has five or more properties. You can force it to a certain view by piping the results to 'Format-List' or 'Format-Table' (be careful, though, because this will make it so you can't usefully pipe those results to anything else; the Format-* cmdlets will make it so your objects are pretty much only good for looking at in the host).

    Now, that's how it works when you're interactively displaying one item at a time. If more than one object is coming out at a time, the formatting system doesn't follow the previous rules (it still follows rules, though). If two objects come out, it will format them both the way that it formatted the first one. This means that if an object that PS would normally format as a list comes out, and an object that it would normally format as a table comes out right after it, they will both be formatted as a list. To see this in action, open an ISE session and run the following lines one at a time (or in the console paste the lines in one at a time):

    # Run these first two commands to define two objects
    $List = New-Object PSObject -Property @{ a = 1; b = 2; c = 3; d = 4; e = 5 }
    $Table = New-Object PSObject -Property @{ a = 1; b = 2; c = 3; d = 4 }
    
    # Run this by itself, it will show as a list b/c there are at least
    # five items:
    $List
    
    # Run this by itself, it will show as a table b/c there are less than
    # five items:
    $Table
    
    # If you run this they will both show as a table b/c the first object 
    # comes out as a table. You will also be missing the 'e' column since 
    # the first object didn't show that
    $Table; $List
    
    # If you run this, they will both show as a list b/c the first object 
    # comes out as a list. All properties will show for each object
    $List; $Table
    

    So, if you're not interested in keeping your objects as true objects that can be worked with later (using ForEach-Object, Where-Object, Export-Csv, etc), then you could do this to force the output as a table:

    $Objects | Format-Table

    • Marked as answer by Nitinswa Sunday, January 5, 2014 5:02 AM
    Saturday, January 4, 2014 8:43 PM
  • Hi Guys,

    Thanks for the response , your answers are spot on.

    The problem was in the first part of the script when i  was checking the availability of PSsnapin for Microsoft.Exchange in Shell in Try , Catch , Finally . So the get-Pssnapin printed results in list format like below

    PS E:\New folder> Get-PSSnapin -Name *Microsoft.Exchange*

    Name        : Microsoft.Exchange.Management.PowerShell.E2010
    PSVersion   : 1.0
    Description : Admin Tasks for the Exchange Server

    And that was setting the Psobject format as list , As i changed the view of that output by piping it to FT , i got below table .

    PS E:\New folder> Get-PSSnapin -Name *Microsoft.Exchange* | ft

    Name                           PSVersion            Description
    ----                           ---------            -----------
    Microsoft.Exchange.Manageme... 1.0                  Admin Tasks for the Exchang...

    Now PSobject gets printed as table too .

    Thanks Rohn and JRV

    Sunday, January 5, 2014 5:01 AM