none
Expand PSObject output RRS feed

  • Question

  • Hello, this is the code and when I run it, I get the output shown further down.

    $color = Get-Random -InputObject "neon", "green", "blue"
    $alpha = Get-Random -InputObject 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N' -Count 5
    $output = New-Object psobject -Property @{
        'color' = $color
        'alpha' = $alpha
    }
    $output

    My question is how to unwrap/expand the output so that it doesn't show up in curly brackets, instead it shows up as a line like D,F,I,A,C

    Wednesday, September 26, 2018 4:06 AM

Answers

  • You could use a custom expression like the following:

    $color = Get-Random -InputObject "neon", "green", "blue"
    $alpha = Get-Random -InputObject 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N' -Count 5
    $output = New-Object psobject -Property @{
        'color' = $color
        'alpha' = $alpha
    }
    
    $output | Select-Object -Property color, @{name="alpha"; expression={ $_.alpha -join ', '}}

    • Marked as answer by strike3test Saturday, September 29, 2018 7:04 AM
    Thursday, September 27, 2018 11:09 AM

All replies

  • $color = Get-Random -InputObject "neon", "green", "blue"
    $alpha = Get-Random -InputObject 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'
    $output = New-Object psobject -Property @{
       color = $color
       alpha = $alpha
    }
    $output
    


    \_(ツ)_/

    Wednesday, September 26, 2018 4:33 AM
  • Thanks JRV, maybe I didn't phrase my question well, sorry about that. So I want to use the script as-is with -count switch but I want the output on screen expanded, not in curly brackets as you see.

    I want the complete line with alphabets, as many as I put in count, so 5 in this case but not inside curly brackets.

    PS: I can see all 5 alphabets if I do $output.alpha.

    Wednesday, September 26, 2018 11:08 PM
  • What you are asking is not possible.  You must write a custom formatter script to get the format you want.  An array will always display like that.


    \_(ツ)_/

    Wednesday, September 26, 2018 11:14 PM
  • You could use a custom expression like the following:

    $color = Get-Random -InputObject "neon", "green", "blue"
    $alpha = Get-Random -InputObject 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N' -Count 5
    $output = New-Object psobject -Property @{
        'color' = $color
        'alpha' = $alpha
    }
    
    $output | Select-Object -Property color, @{name="alpha"; expression={ $_.alpha -join ', '}}

    • Marked as answer by strike3test Saturday, September 29, 2018 7:04 AM
    Thursday, September 27, 2018 11:09 AM