Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.
If you own the book already, login here to get free, online, searchable access to the entire book's content.
If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".
You want to split a string based on some literal text or a regular expression pattern.
Use PowerShell’s -split
operator to split on a sequence of characters or specific string:
PS > "a-b-c-d-e-f" -split "-c-" a-b d-e-f
To split on a pattern, supply a regular expression as the first argument:
PS > "a-b-c-d-e-f" -split "b|[d-e]" a- -c- - -f
To split a string, many beginning scripters already comfortable with C# use the String.Split()
and [Regex]::Split()
methods from the .NET Framework. While still available in PowerShell, PowerShell’s -split
operator provides a more natural way to split a string into smaller strings. When used with no arguments (the unary split operator), it splits a string on whitespace characters, as in Example 5-3.
PS > -split "Hello World `t How `n are you?" Hello World How are you?
When used with an argument, it treats the argument as a regular expression and then splits based on that pattern.
PS > "a-b-c-d-e-f" -split 'b|[d-e]' a- -c- - -f
If the replacement pattern avoids characters that have special meaning in a regular expression, you can use it to split a string based on another string.
PS > "a-b-c-d-e-f" -split '-c-' a-b d-e-f
If the replacement pattern has characters that have special meaning in a regular expression (such as the .
character, which represents “any character”), use the -split
operator’s SimpleMatch
option, as in Example 5-4.
PS > "a.b.c" -split '.' (A bunch of newlines. Something went wrong!) PS > "a.b.c" -split '.',0,"SimpleMatch" a b c
For more information about the -split
operator’s options, type Get-Help about_split
.
While regular expressions offer an enormous amount of flexibility, the -split
operator gives you ultimate flexibility by letting you supply a script block for a split operation. For each character, it invokes the script block and splits the string based on the result. In the script block, $_
(or $PSItem
) represents the current character. For example, Example 5-5 splits a string on even numbers.
PS > "1234567890" -split { ($_ % 2) -eq 0 } 1 3 5 7 9
When you’re using a script block to split a string, $_
represents the current character. For arguments, $args[0]
represents the entire string, and $args[1]
represents the index of the string currently being examined.
To split an entire file by a pattern, use the -Delimiter
parameter of the Get-Content
cmdlet:
PS > Get-Content test.txt Hello World PS > (Get-Content test.txt)[0] Hello PS > Get-Content test.txt -Delimiter l He o Wor d PS > (Get-Content test.txt -Delimiter l)[0] He PS > (Get-Content test.txt -Delimiter l)[2] o Wor PS > (Get-Content test.txt -Delimiter l)[3] d
For more information about the -split
operator, see “Simple Operators” or type Get-Help about_split
.
“Simple Operators”
Appendix B, Regular Expression Reference