Convert an Array Object to a String in PowerShell

Rohan Timalsina Jan 30, 2023 Dec 30, 2021
  1. Use " " to Convert an Array Object to String in PowerShell
  2. Use the join Operator to Convert an Array Object to a String in PowerShell
  3. Use the Explicit Conversion to Convert an Array Object to a String in PowerShell
  4. Use the Output Field Separator Variable to Convert an Array Object to a String in PowerShell
  5. Use [system.String]::Join(" ", $array) to Convert an Array Object to a String in PowerShell
Convert an Array Object to a String in PowerShell

PowerShell has multiple data types: string, integer, array, boolean, DateTime, etc. This tutorial will introduce different methods to convert an array object to string in PowerShell.

Use " " to Convert an Array Object to String in PowerShell

Double inverted commas " " denote a string in PowerShell. You can use the same method to convert an array object to a string data type.

Suppose we have an array object $address.

$address = "Where", "are", "you", "from?"

You can check the data type using the GetType() method.

$address.GetType()

Output:

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

When you encode the array variable with " ", it will be converted into a string.

"$address"

Output:

Where are you from?

Check the data type:

"$address".GetType()

Output:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

Use the join Operator to Convert an Array Object to a String in PowerShell

The join operator is another method to convert an array object to a string. It helps to join the array of items with specific characters, numbers, or letters.

$address -join "+"

Output:

Where+are+you+from?

Let’s check the data type.

$a = $address -join "+"
$address.GetType().Name

Output:

Object[]

Use the Explicit Conversion to Convert an Array Object to a String in PowerShell

You can explicitly convert an array object to a string by casting it to a string data type.

For example:

[string]$address

Output:

Where are you from?

Use the Output Field Separator Variable to Convert an Array Object to a String in PowerShell

The Output Field Separator variable $OFS helps convert an array object to a string in PowerShell.

You can follow the same steps below.

$OFS = '-'
"$address"

Output:

Where-are-you-from?

Use [system.String]::Join(" ", $array) to Convert an Array Object to a String in PowerShell

The following command also joins the items of an array to convert them to a string.

[system.String]::Join(" ", $address)

Output:

Where are you from?
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell String

Related Article - PowerShell Array