Sign up for our newsletter.

I agree to this site's Privacy Policy.

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

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 "-"

The split results in an array:

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

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")

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 "_"

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
#add elements to the array
$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: * ) -

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

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog, on Twitter at and on Google Plus (http:/

comments powered by Disqus

Reader Comments:

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above