Author Posts

August 22, 2018 at 9:55 am

Hi!

I am trying to do something like this:

$array = "Hello, every, body"
$array -split ','

When i do this, it display it in this way:
Hello
Every
Body

I want them on ONE line. Any suggestions? This is a problem for other stuff too..

August 22, 2018 at 10:05 am

This is just a string though, not an array:

$array = "Hello, every, body"

This would be an array:

$array = "Hello", "every", "body"

To turn the array back into a string you could do this:

$array -Join " "

August 22, 2018 at 10:17 am

Variable Array is not actually an array here, so you can do a replace with space

$array -replace ',',''

if its an array of string on integers you can do it like

"$array"

String conversion does the magic.

August 22, 2018 at 12:01 pm

-split turns a string into an array. It just displays it as three lines. Maybe you want to replace the commas with spaces?

PS /Users/js> $array = "Hello,every,body"
PS /Users/js> $array = $array -split ','

PS /Users/js> $array.count                                                                                       
3
PS /Users/js> $array[0]                                                                                          
Hello
PS /Users/js> $array[1]                                                                                          
every
PS /Users/js> $array[2]                                                                                          
body

PS /Users/js> $array = "Hello,every,body"
PS /Users/js> $array = $array -replace ',',' '                                                                   
PS /Users/js> $array                                                                                             
Hello every body

August 22, 2018 at 12:44 pm

I was just wondering, what if you have $array = "one","two","three", PowerShell display them on three lines. How do you get the output on just one line?

August 22, 2018 at 2:24 pm

What exactly are you trying to accomplish? The array is 3 items, so it's 3 rows. Powershell displays rows on each line horizontally by default. Cmdlets like Format-Table or Format-List manipulate the columns, but everything is still displayed on each row horizontall. If you want to see them vertically, as this the other folks have said, you need to convert the array to a string. Outside of that, you are going against the display behavior in Powershell and modifying format XML files, which is complicated because you are going against default display behavior.

August 22, 2018 at 2:30 pm

PS C:\users\superuser> $array -join ' '
one two three

# or
PS C:\users\superuser> "$array"
one two three