An easy way to output an array in a single line

10,371

Yes, using -join:

$a -join ','
Share:
10,371
Vesper
Author by

Vesper

I'm a hobby programmer, I specialize in common mistakes and blind debugging. I live in Moscow, Russia, and I'm pretty happy with it.

Updated on June 04, 2022

Comments

  • Vesper
    Vesper almost 2 years

    I have an array in a line of $a=@(1,2,3,4,56). When I do Write-Debug $a I get an exception in a line of "Array is not a String". If I do $a | out-string | write-debug I get a list of values in a column. I can write a function that'll write an array in a row (or a string without newlines), but I'd like to know if I'm inventing a bicycle that exists in Powershell, so I can use an existing solution.

    Is there a built in function or a not too fancy one-liner to display an array in a format similar to (1,2,3,4,56)?

  • Vesper
    Vesper over 8 years
    Heh. Easy frag. Thanks.
  • Master Azazel
    Master Azazel over 5 years
    awesome! way cleaner then doing a foreach imo
  • Atorian
    Atorian over 2 years
    In case you want to output as multiple lines, $a -join "`n" does the job.