PowerShell Split Operator
In another tutorial we saw how we are able to use Join operator and what we are able to do with it. In this tutorial we will look into PowerShell Split Operator, how we are able to use it and what we can do with it.
The split operator allows you to split a string or multiple strings into sub-strings based on a delimiter that you provide. If you do not specify and delimiter, the default one is white space (” “). Split operator by default will return all sub-strings. You are able to specify maximum number of sub-strings. If you specify a number that is less that the number of sub-strings, then the last sub-string will combine all the rest of sub-strings above that number.
The below examples show us the default behaviour of the split operator without specifying any delimiter other than the default.
Code:
-split "Monday Tuesday Wednesday Thursday Friday Saturday Sunday" $a = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday" -split $a
Output:
As you can see above, the string does not matter if you save it in a variable or not. The string is split based on the default delimiter which is white space (” “).
[adinserter name=”In Article”]
Delimiter
On the next examples we will use delimiter in order to split our string into sub-strings. Each example is a different case with different result. On the first example, dash ( – ) is used as a delimiter to split the string. As you will see the delimiter is omitted from the output.
Code:
$a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday" $a -split "-"
Output:
If you want to keep the delimiter in the output when you will use -split
, then you need to enclose it in parentheses ( )
. The below examples will show how this can be done.
Code:
$a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday" $a -split "(-)"
Output:
As you can see above, we are able to keep the delimiter in the output. There are some cases that we might need to use more that one character as a delimiter and keep only part of it in our output. If you want to keep only part of a delimiter, then you need to enclose only that part in parentheses ( )
.
Code:
$a = "Monday:-:Tuesday:-:Wednesday:-:Thursday:-:Friday:-:Saturday:-:Sunday" $a -split ":(-):" $a = "Monday:-Tuesday:-Wednesday:-Thursday:-Friday:-Saturday:-Sunday" $a -split ":(-)"
Output:
[adinserter name=”In Article”]
Max-Strings
As it was mentioned before the default behaviour of -split operator is to show all sub-strings if it is not specified differently. The below examples use max sub-strings on the output. The first example will not keep the delimiter in the output and the second examples will keep the delimiter in the example.
Code:
$a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday" $a -split "-" , 5 $a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday" $a -split "(-)" , 5
Output:
[adinserter name=”In Article”]
Multiple Strings
If you have multiple strings that you need to split them, you need to use a specific format in order to split both strings. The below explanation is as provided by Microsoft.
The unary split operator (-split <string>) has higher precedence than a comma. As a result, if you submit a comma-separated list of strings to the unary split operator, only the first string (before the first comma) is split.
Please check below examples:
Code:
$a = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday" $b = "1 2 3 4 5 6 7" -split $a,$b
Output:
Code:
$a = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday" $b = "1 2 3 4 5 6 7" $a , $b -split " " $a = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday","1 2 3 4 5 6 7" -split $a $a = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday" $b = "1 2 3 4 5 6 7" -split ($a,$b)
Output:
[adinserter name=”In Article”]
RegexMatch Split
You are also able to split a string based on conditions. This can be useful if you need to use multiple delimiters or if you want to proceed split the string differently under specific conditions. Lets check the below examples.
Code:
$a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday" $a -split {$_ -eq "d"} $a = "Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday" $a -split {($_ -eq "e") -or ($_ -eq "d")}
Output:
As you can see above you are able to have a regex for delimiters. Please note that you are only able to use only one character in order to work.
Code:
$x = 3 $a = "Monday-Tuesday:Wednesday-Thursday:Friday-Saturday:Sunday" $a -split {if ($x -gt 4) {$_ -eq "-"} else {$_ -eq ":"}} $x = 5 $a = "Monday-Tuesday:Wednesday-Thursday:Friday-Saturday:Sunday" $a -split {if ($x -gt 4) {$_ -eq "-"} else {$_ -eq ":"}}
Output:
As you can see above you are able to have statements in order to specify a delimiter based on specific conditions. In the above examples we are checking the value of $x
in order to specify the delimiter. If the value of $x
is more than 4 then the delimiter is -
else the delimiter is :
.
I hope the tutorial about PowerShell Split Operator is helpful.
Please let me know your comments and thoughts.
You feedback is appreciated.
[adinserter name=”In Article”]
Related Links:
[adinserter name=”Matched-Content”]


Leave a Reply