Powershell

How do I Convert an Array Object to a String in PowerShell?

PowerShell support several data types, including string, integer, boolean, or arrays. More specifically, An array is a collection of items with identical data types stored at contiguous locations in memory. While the string is the collection of words presented as a meaningful text. Arrays are beneficial in maintaining a large number of data by storing it into a single variable. It can be transformed into a PowerShell string.

This tutorial will present a guide to resolve the mentioned query.

How to Convert an Object’s Array into a PowerShell String?

These given approaches can be employed to convert an object array into a PowerShell string:

Method 1: Convert an Array Object to a PowerShell String Using “[String]$array”

In this method, the array object will be converted to a string using the “[String]$array” method. For instance, overview the given example.

Example

Now, we will convert the array of objects into a string using the “[string]$array” method:

> $Obj = "Car", "Truck", "Bike", "Cycle"
> [string]$Obj

In the above-mentioned code:

  • First, we have created an array and added various objects separated by commas.
  • After that, we used the “[string]” parameter along with the array name, which is “$Obj”, to convert it to a string:

It can be observed that the values stored in the specified array have been printed out as a string.

Method 2: Convert an Array Object to a PowerShell String Using Inverted Commas “ ”

The double inverted commas “ ” are also utilized to convert an array object to a string.

Example

Execute the following lines of code in PowerShell:

> $Obj = "Car", "Truck", "Bike", "Cycle"
> "$Obj"

In the stated code example:

  • First, we created the array and stored several objects in it.
  • After that, we invoked the array name within inverted commas such as “$Obj”.
  • Lastly, calling the array of objects will convert it to the string:

Method 3: Convert an Array Object to a PowerShell String Using [system.String]::Join(” “, $array)

This approach utilizes the “system.String” class for converting the array object “$array” to string after joining them with the help of its Join() method.

Example

In the below-given example:

  • First, we created an array of objects and then assigned several objects.
  • After that, invoke the “[system.String]::Join(” “, $Obj)” method to concatenate the objects inside the array and will add the spaces between the objects.
  • Last operation is about the array object to string conversion:
> $Obj = "Car", "Truck", "Bike", "Cycle"
> [system.String]::Join(" ", $Obj)

Method 4: Convert an Array Object to a PowerShell String Using Join Operator

The “-join” operator is also used to convert the array object to a PowerShell string. It specifically joins the characters, numbers, or spaces with an array of items.

Example

This example converts an array object using the “-join” operator:

> $Obj = "Car", "Truck", "Bike", "Cycle"
> $Str = $Obj -join " "
> $Str

In this example, we have used the “-join” operator to concatenate the list of objects and add the space within inverted commas to add the space between the objects when the string is created:

It can be observed that the specified array object has been successfully converted to a string.

Conclusion

An array object can be converted to a string by using several methods. These methods include [String]$array, [system.String]::Join(” “, $array) or join operator. These methods first take the objects of an array, concatenate them and finally add the spaces between to convert. This tutorial has presented a guide to convert an array object to a PowerShell string.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.