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.
Now, we will convert the array of objects into a string using the “[string]$array” method:
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.
Execute the following lines of code in PowerShell:
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.
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:
> [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.
This example converts an array object using the “-join” operator:
> $Str = $Obj -join " "
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.
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.