PowerShell Array to String

Convert Array Object to String in PowerShell

Using Double Quotation Marks

Use double quotation marks to convert array object to string in PowerShell.

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.

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.

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.

Using -join Operator

Use the -join operator to convert an array object to a string.

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.

Using Join() Function

Use the Join() function to convert an array object to a string.

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.

Using Out-String Cmdlet

Use the Out-String cmdlet to convert an array object to a string.

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.

Using $OFS Variable

Use the $OFS variable to convert an array object to a string.

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.

That’s all about PowerShell Array to String,

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *