PowerShell Split String Operation With Examples – POFTUT

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 caseinsensitive. We will provide space as separator in this example.

$ "my test system is".split(" ")
Specify Separator
Specify Separator

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.

$separators=(" ",".")

Then we provide $separators variable to the split function.

"my te.st sys.tem is".split($separators)
Split With Multiple Separators
Split With Multiple Separators

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.

 "my te.st sys.tem is".Split("[ts]")
Split According To Regex
Split According To Regex

Split Mac Address

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

"12-34-56-78-9A-BC".Split("-")
Split Mac Address
Split Mac Address

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

LEARN MORE  How to Lookup OUI (Organizationally Unique Identifier) In Linux?

Leave a Comment