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.
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.
$ "my test system is".split(" ")
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.
"my te.st sys.tem is".split($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
s characters as separator by specifying in regex.
"my te.st sys.tem is".Split("[ts]")
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.