Using the Split Method in PowerShell

Using the Split Method in PowerShell

  • Comments 7
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Split method in Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here. Dude, dude, dude, (or dudette, as the case may be) I still cannot find my teapot after our move. At least I found my kettle to heat water so I can make tea, but unfortunately, without a teapot, I am stuck drinking “bagged” tea leaves. It is not a major crisis yet, but I do miss blending my own teas with my own herbs. And of course, my various tins of teas and herbs are sitting here, just waiting for me to locate the teapot. On the bright side, I did pick up some nice tea bags when the Scripting Wife and I were in Europe, so it is not a total loss. In fact, I got a rather nice oolong tea in little triangle sachets that is actually not bad.

Dude, let's split

The Split method from the System.String class is not a static method. This means that when I have a string, I can gain access to the Split method. But if I do not have a string, I cannot access it. The MSDN documentation for the String.Split Method lists six overloads for this method. So I have a lot of flexibility on how I might want to use the method. I will not discuss all six overloads today, but I will discuss the three main ways of using the method:

  • Split on an array of Unicode characters
  • Split on an array of strings with options
  • Specify the number of elements to return

The additional ways of calling the method will behave in a similar fashion.

Split on an array of Unicode characters

One of the cool things about the Split method is that it will accept an array of things upon which to split. Therefore, I can split on one or more Unicode characters.

Note  A handy list of Unicode characters and their associated code values appears on Wikipedia. When using them in Windows PowerShell, remove the U+ and change it to 0X, then call it with [Char]. For example, the right curly brace is [char]0X007D.

The first thing I need is a string with Unicode characters embedded inside it. So here is my string:

$string = "a powershell $([char]0x007B) string $([char]0x007D) contains stuff"

Now I need to create an array of two characters to use to split the string. Here is what I come up with the first time:

$string.Split("{,}")

And this command works. Here is a screenshot of the string, my split characters, and the associated output:

Image of command output

What is cool is that I can do this same thing in reverse. Here is an example of reversing the process:

PS C:\> $string2 = "a powershell {string} contains stuff"

PS C:\> $string2.Split([char]0x007B, [char]0x007D)

a powershell

string

 contains stuff

PS C:\>

Split on an array of strings with options

The option to specify an array of strings to use for splitting a string offers a lot of possibilities. The StringSplitOptions enumeration also offers a way to control the return of empty elements. The first thing I need to do is to create a string. Here is my string:

$string = "This string is cool. Very cool. It also rocks. Very cool. I mean dude.Very cool. Very cool."

Now, I need to specify a separator. To do this, I will assign an array of strings to the $separator variable as shown here:

$separator = "Very cool.","."

Now, I need to create my StringSplitOption. There are two options: None and RemoveEmptyEntries. The first time I run the command, I will remove the empty entries. So here is that command:

$option = [System.StringSplitOptions]::RemoveEmptyEntries

The command and the output from the command appears here:

Image of command output

When I change the option to None, I see that there is one extra space at the end of the line. This happens because the words “Very Cool.” appear twice at the end of the string. This is shown here:

Image of command output

Specify the number of elements to return

It might be useful to return only a certain number of elements from a string. Here is an example using a string array as a separator:

$string = "This string is cool. Very cool. It also rocks. Very cool. I mean dude. Very cool. Very cool."

$separator = "Very cool.","."

$option = [System.StringSplitOptions]::RemoveEmptyEntries

$string.Split($separator,2, $option)

The first time I ran the command, it generated an error message. The reason is that I added the number of elements to return to the end of the method call. The last position is for the Split option. Here are the commands and the associated output:

Image of command output

That is all there is to using the Split method. String Week will continue tomorrow when I will talk about more string stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks Ed!

    I wish I would have known about the number between the string and the option before I wrote this blog post http://vernanderson.wordpress.com/2014/03/07/split-method-gotchas/ Now I know exactly why that happened.

  • @Vern Awesome! I am glad that you found the article useful and that you learned something. WOOHOO!!!

  • Hi scripting guy it is a well done job

  • Hi, I want to generate user e-mail address from a script. For example user name (Michael), lastname in two phase (John Smith). How can I generate to have output: michaeljs@xxxx.com. That's pick first letter of first word and first letter of second word from lastname. I have it like this for now: $e-mail = $firstname + $lastname.substring(0,1), output is: michaelj@xxxx.com. How can I grab S from John Smith?

  • Nice, didn't know about returning a certain number of elements... that's helpful.

  • Hi Joey,
    I'm sure that there many many ways to do that , and i have just started learning powershell like two weeks ago literally

    if you read this topic again in the future , this is one way to solve your dilema

    #initial variables
    $firstname = "Michael"
    $lastname = "John Smith"

    #this will return an array
    $alastname = $lastname -split " "

    #you can use the array indexes to access "John" and "Smith" separately
    $email = $firstname + $alastname[0].substring(0,1) + $alastname[1].Substring(0,1)

    #Have fun!