213

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?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
jrara
  • 16,239
  • 33
  • 89
  • 120

6 Answers6

346
$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
Zombo
  • 1
  • 62
  • 391
  • 407
Roman Kuzmin
  • 40,627
  • 11
  • 95
  • 117
  • 14
    For the un-initiated (like me) `$ofs` is documented [here](http://stackoverflow.com/documentation/powershell/5353/automatic-variables/19045/ofs) – Liam Oct 04 '16 at 16:27
  • 20
    Stackoverflow Documentation has been shut down so Liam's link is dead. Here's another explanation of $OFS, the Output Field Separator: https://blogs.msdn.microsoft.com/powershell/2006/07/15/psmdtagfaq-what-is-ofs/ – Simon Elms Nov 05 '17 at 08:30
  • Why is it called OFS ? It's a weird name considering it's been called IFS on standard shell for decades and decades. – Johan Boulé May 30 '18 at 15:00
  • 1
    @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). – Martin Bonner supports Monica Sep 12 '18 at 14:18
  • @martin bonner : thanks, it makes sense now. I don't know how I messed this up. – Johan Boulé Sep 13 '18 at 22:42
  • 1
    Official documentation is here: https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Preference_Variables.md#ofs – testworks Oct 03 '19 at 06:07
  • 1
    Seems you can't edit comments after a certain time. Here's a couple of working links: [learn.microsoft.com PowerShell 7.1](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.1#ofs) [GitHub PowerShell 7.1](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/4295a917fcb0140aa96426cc2ef8f861771d36cf/reference/7.1/Microsoft.PowerShell.Core/About/about_Preference_Variables.md#ofs) – testworks Sep 06 '21 at 03:26
  • "$OFS is a special variable that contains the string to be used as the Ouptut Field Sperator. **This string is used when an array is converted to a string. By default, this is ” ” but you can change it.**" – Prid Oct 11 '21 at 20:58
48

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeremy Saunders
  • 489
  • 4
  • 2
  • 5
    FYI: just doing `$a` would have the same effect as `$a | out-string` – JohnLBevan Oct 12 '14 at 01:40
  • 9
    @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. – rojo Feb 26 '16 at 13:44
  • 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. – Minkus Jun 14 '23 at 11:13
24
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
CodeFox
  • 3,321
  • 1
  • 29
  • 41
TCC
  • 241
  • 2
  • 2
13

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

Zombo
  • 1
  • 62
  • 391
  • 407
  • 2
    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 – Martin Konopka Sep 18 '19 at 11:18
  • `$input` variable https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.3#input – Winand Apr 08 '23 at 16:39
4

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
Ameer Deen
  • 704
  • 8
  • 20
0
$a = "This", "Is", "a", "cat"

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

Write-Host $sent
Underverse
  • 1,271
  • 21
  • 32