Table of Contents
Using Double Quotation Marks
Use double quotation marks to convert array object to string in PowerShell.
1 2 3 4 5 6 |
$arrayObject = "How", "are", "you?" $arrayString = "$arrayObject" $arrayObject.GetType() $arrayString.GetType() |
1 2 3 4 5 6 |
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array True True String System.Object |
First, we created an array object and saved it in the $arrayObject
variable; we can also create an array object using the array operator (@()
), for instance, $arrayObject = @("How", "are", "you?")
. Next, we enclosed the $arrayObject
variable within double quotes and saved the updated value in the $arrayString
variable.
Afterwards, we chained the GetType()
method with $arrayObject
and $arrayString
to know their data types. For example, see the Name
column in the above output; we have successfully converted the array object to a string. Now, we can print the value of $arrayString
to see what the converted string looks like; see the following example.
1 2 3 |
$arrayString |
1 2 3 |
How are you? |
From now on, we will only discuss the function, variable, cmdlet, and operator that we used to convert the array object to a string; we will not explain the array creation and GetType()
method as we have already covered them in this section. So, let’s continue with the remaining sections.
Using Type Casting / Explicit Conversion
Use type casting (explicit conversion) to convert an array object to a string.
1 2 3 4 5 6 |
$arrayObject = "How", "are", "you?" $arrayString = [string]$arrayObject $arrayObject.GetType() $arrayString.GetType() |
1 2 3 4 5 6 |
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array True True String System.Object |
Here, we used the explicit conversion of an array object to a string. We can also call it typecasting because we are casting (changing) the data type from one to another.
We specified the [string]
immediately followed by $arrayObject
to cast the data type from array object to string and saved it $arrayString
variable. So now, the $arrayString
has the following string.
1 2 3 |
$arrayString |
1 2 3 |
How are you? |
Using -join
Operator
Use the -join
operator to convert an array object to a string.
1 2 3 4 5 6 |
$arrayObject = "How", "are", "you?" $arrayString = $arrayObject -join " " $arrayObject.GetType() $arrayString.GetType() |
1 2 3 4 5 6 |
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array True True String System.Object |
For this section, we used the -join
operator, which joins the array elements using the specified separator. The separator can be a letter, number, or a specific character; however, we used a single white space to join/concatenate array elements. Now, the value of $arrayString
looks as follows.
1 2 3 |
$arrayString |
1 2 3 |
How are you? |
Further reading:
Using Join()
Function
Use the Join()
function to convert an array object to a string.
1 2 3 4 5 6 |
$arrayObject = "How", "are", "you?" $arrayString = [system.String]::Join(" ", $arrayObject) $arrayObject.GetType() $arrayString.GetType() |
1 2 3 4 5 6 |
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array True True String System.Object |
This code fence is similar to the previous one, where we used the -join
operator. But here, we used the Join()
function, which also did the same, joining all elements of the array using the specified separator. So now, $arrayString
looks like the below.
1 2 3 |
$arrayString |
1 2 3 |
How are you? |
Using Out-String
Cmdlet
Use the Out-String
cmdlet to convert an array object to a string.
1 2 3 4 5 6 |
$arrayObject = "How", "are", "you?" $arrayString = $arrayObject | Out-String $arrayObject.GetType() $arrayString.GetType() |
1 2 3 4 5 6 |
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array True True String System.Object |
Here, we used Out-String cmdlet which outputs the received input objects as a string. We used pipe sign (|
) to forward the data of $arrayObject
to the Out-String
.
This approach displays an output which differs from the previous methods. It converts every array element to a string; see the following output.
1 2 3 |
$arrayString |
1 2 3 4 5 |
How are you? |
Using $OFS
Variable
Use the $OFS
variable to convert an array object to a string.
1 2 3 4 5 6 7 |
$arrayObject = "How", "are", "you?" $OFS = ',' $arrayString = "$arrayObject" $arrayObject.GetType() $arrayString.GetType() |
1 2 3 4 5 6 |
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array True True String System.Object |
This code example is similar to the first section, where we used double quotation marks to convert an array object to a string. However, with $OFS
(output field separator), we can use a separator of our choice without using the -join
operator or Join()
function.
After using the $OFS
variable, all casts will have a separator that we assigned to the $OFS
variable until $OFS
changes. This time, the $arrayString
will look as below.
1 2 3 |
$arrayString |
1 2 3 |
How,are,you? |
That’s all about PowerShell Array to String,