Prof. Powershell

Pick Up the Pieces with the Split, Join Operators

PowerShell's object nature is fine most of the time, but sometimes we need to dig deep and use the Split and Join operators to break apart strings and put them back together again.

Even though I've stressed for years about PowerShell's object nature there are times when we need to pick apart something into component pieces and other times put those pieces back together. For those situations we can use the operators -Split and -Join. These operators can be used in ways that normally don't come to mind as you'll see.

Here's a sample string:

PS C:\> $s="Rock and Roll Will Never Die"

This can easily be split using the default delimiter of the space:

PS C:\> -split $s
Rock
and
Roll
Will
Never
Die

Normally we don't think of starting a command with an operator like this. Even though it looks odd, it works. If you need to specify a delimiter, use this syntax:

PS C:\> $s="Rock-and-Roll-Will-Never-Die"
PS C:\> $s -split "-"
Rock
and
Roll
Will
Never
Die

The split results in an array:

PS C:\> $a=$s -split "-"
PS C:\> $a[0..2]
Rock
and
Roll

You could even split on a regular expression pattern. Consider a sample line like this:

PS C:\> $d="I am data123more data456still more data"

Now we'll split it using a regex pattern as the delimiter:

PS C:\> $d -split "\d{3}"
I am data
more data
still more data

Of course what we can take apart we can put back together using the -Join operator. I still have $a, so let's join it back. The default delimiter for -join is no delimiter at all:

PS C:\> -join ("d:\files","\","Fooby",".txt")
d:\files\Fooby.txt

There are better ways of building a path. This is merely an example of the operator in action. If you need to specify a delimiter, use this syntax:

PS C:\> $a -join " "
Rock and Roll Will Never Die

Or perhaps I want something else:

PS C:\> $a -join "_"
Rock_and_Roll_Will_Never_Die

Let's wrap up with a short code sample that uses both operators to construct a random string you might use as a password:

$x=Read-host "enter a word with upper and lower case"
$y=Read-host "enter a comma separated list of numbers"
$z=Read-host "enter a few characters separated by spaces"

#create an empty array
$arr=@()
#add elements to the array
$arr+=$x.ToCharArray()
$arr+=$y -split ","
$arr+=-split $z

#put it together
-join ($arr | get-random -count $arr.count)

The code prompts the user to enter some values which are added to an array. A few items are split into their component elements. The end result is an array of letters, numbers and characters. The -Join operator takes a random order of these elements and joins them into a string. Here's what it looks like in action:

enter a word with upper and lower case: Power
enter a comma separated list of numbers: 1,5,13
enter a few characters separated by spaces: * ) -
w131o*5e)-rP

Be sure to read the help topics About_Split and About_Join for more details.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events