PowerShell Split String Operation With Examples


Powershell provides string type methods to the string objects. Split is one of them. Split method is used to split string into separate parts or a string array. In this tutorial we will look different usage types and examples of Split.

Specify Separator

While splitting a text or string we can provide a separator to split them. We will provide as argument to the split function. Function names are incase sensitive. We will provide space as separator in this example.

Each separated element is printed in a new line.

Split With Multiple Separators

In some situations there may be more than one separator. Another useful feature of split function is we can specify multiple separators by adding more split functions and related separator in a string mode. In this example we will define two separator like space and point.

First we create our separator variable.

Then we provide $separators variable to the split function.

Split According To Regex

For more structured but complex strings and text regex based separator can be used. In this example we will use t and s characters as separator by specifying in regex.

Split According To Regex

Split According To Regex

Split Mac Address

Another useful example will be splitting the mac address into hexadecimal parts.

We get all parts of the mac address in a new line. If we need we can loop in them using foreach.

LEARN MORE  Killing Linux Processes With killall Command

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)