Home » How to Convert Array to String in PowerShell

How to Convert Array to String in PowerShell

You can use multiple ways to convert an array to a string in PowerShell.

The following methods show how you can do it with syntax.

Method 1: Using the Out-String cmdlet

$languages = @("PowerShell","Python","Java","C#")

$langString = $languages | Out-String

Write-Output $langString

This example will convert an array to a string.

Method 2: Using the casting

$languages = @("PowerShell","Python","Java","C#")

$langString = [string]$languages

Write-Output $langString

This example will cast the array to a string.

The following examples show how you can use these methods.

Convert Array to a String Using the Out-String cmdlet in PowerShell

The easy way to convert an array to a string is to use the Out-String cmdlet in PowerShell.

# define an array
$languages = @("PowerShell","Python","Java","C#")

# convert array to string
$langString = $languages | Out-String

# output the string
Write-Output $langString

Output:

PowerShell convert array to string using out-string
PowerShell convert array to string using out-string

In this script, the $languages variable contains the array of strings. We then use Out-String cmdlet to convert the array to a string.

Finally, using the Write-Output cmdlet in PowerShell, output the string.

Convert Array to String Using Casting

Another way to convert an array to a string is by casting the array to a string.

# define an array of strings
$languages = @("PowerShell","Python","Java","C#")

# cast the array to a string
$langString = [string]$languages

# write the output
Write-Output $langString

Output:

PowerShell convert array to string using casting
PowerShell convert array to string using casting

In this script, the $languages variable contains the array of strings. We then cast the array to a string and print them to a console using the Write-Output cmdlet.

Conclusion

I hope the above article on converting an array to a string in PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.