232

How can I convert an array object to string?

I tried:

$a = "This", "Is", "a", "cat"
[system.String]::Join(" ", $a)

with no luck. What are different possibilities in PowerShell?

2
  • 6
    See my answer but your code works just fine, too. Why do you say "with no luck"? Commented Oct 11, 2011 at 9:30
  • 4
    Sorry, yes, it does seem to work, I think I messed up something when I tested this. Commented Oct 11, 2011 at 9:41

6 Answers 6

366
$a = 'This', 'Is', 'a', 'cat'

Using double quotes (and optionally use the separator $ofs)

# This Is a cat
"$a"

# This-Is-a-cat
$ofs = '-' # after this all casts work this way until $ofs changes!
"$a"

Using operator join

# This-Is-a-cat
$a -join '-'

# ThisIsacat
-join $a

Using conversion to [string]

# This Is a cat
[string]$a

# This-Is-a-cat
$ofs = '-'
[string]$a
Sign up to request clarification or add additional context in comments.

8 Comments

For the un-initiated (like me) $ofs is documented here
Stackoverflow Documentation has been shut down so Liam's link is dead. Here's another explanation of $OFS, the Output Field Separator: blogs.msdn.microsoft.com/powershell/2006/07/15/…
@JohanBoulé : Because using the first character of the Input Field Separator as the Output Field Separator is a nasty hack - and doesn't allow separating fields with multi-character strings. (Awk for example has FS and OFS variables).
Seems you can't edit comments after a certain time. Here's a couple of working links: learn.microsoft.com PowerShell 7.1 GitHub PowerShell 7.1
|
51

I found that piping the array to the Out-String cmdlet works well too.

For example:

PS C:\> $a  | out-string

This
Is
a
cat

It depends on your end goal as to which method is the best to use.

3 Comments

FYI: just doing $a would have the same effect as $a | out-string
@JohnLBevan Not always. ($a | out-string).getType() = String. $a.getType() = Object[]. If you're using $a as an argument to a method expecting a string (such as invoke-expression for example), then $a | out-string has a clear advantage.
Tried this - note that Out-String can truncate long lines based on the characteristics of the host program, so you may experience long variables been wrapped to another line - see the Width parameter.
28
1> $a = "This", "Is", "a", "cat"

2> [system.String]::Join(" ", $a)

Line two performs the operation and outputs to host, but does not modify $a:

3> $a = [system.String]::Join(" ", $a)

4> $a

This Is a cat

5> $a.Count

1

Comments

16

From a pipe

# This Is a cat
'This', 'Is', 'a', 'cat' | & {"$input"}

# This-Is-a-cat
'This', 'Is', 'a', 'cat' | & {$ofs='-';"$input"}

Write-Host

# This Is a cat
Write-Host 'This', 'Is', 'a', 'cat'

# This-Is-a-cat
Write-Host -Separator '-' 'This', 'Is', 'a', 'cat'

Example

3 Comments

This is amazing trick. The Example does not work anymore, and even though this answer is 5 years old, I try to explain what I learned today. The $ofs is Output Field Separator variable which is used when an array is converted into string for output. Here it is set in the script block returning string value of the input (array from the pipe) which gets executed by the command &. I didn't know about $ofs before, as well as & accepting a script block as argument
This one was difficult to find an answer for. Thank you! This will pull a list of words from a file and then pass them to a command as a list of arguments: gc file.txt | & { command $input }
5

You could specify type like this:

[string[]] $a = "This", "Is", "a", "cat"

Checking the type:

$a.GetType()

Confirms:

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String[]                                 System.Array

Outputting $a:

PS C:\> $a 
This 
Is 
a 
cat

Comments

0
$a = "This", "Is", "a", "cat"

foreach ( $word in $a ) { $sent = "$sent $word" }
$sent = $sent.Substring(1)

Write-Host $sent

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.