Join


Definition: Returns a string created by joining a number of substrings contained in an array.

 

There will be times (trust us, there will be) when you want to take all the values in an array and transform them into a single string. In Windows PowerShell you can do that using the System.Stringclass and the Joinmethod. For example, in the first of the following two commands we assign the letters h-e-l-l-o to an array variable named $a; in the second command we use the Join method to combine those values into a string variable named $b. Notice that we pass Join two parameters: the separator (that is, the character we want inserted between each array item) and the array to be joined. In this example we don’t want anycharacter inserted between items, so we simply pass an empty string:

 

$a ="h","e","l","l","o"
$b = [string]::join("", $a)

 

When you run this command and then echo back the value of $b you should get the following:

 

hello

 

What if we wanted to place a \ between each item? Then we’d use this command:

 

$b = [string]: :join("\", $a)

 

In turn, the value of $b would be equal to this:

 

h\e\l\l\o

 

Cool, huh?