How to Split a String in PowerShell (With Examples)

To split a string in PowerShell, use the -split operator with the delimiter. It returns an array, with one item per piece:

$cities = 'Austin,Denver,Seattle,Boston' -split ','

$cities
"Items: $($cities.Count), first: $($cities[0]), last: $($cities[-1])"

Output:

Austin
Denver
Seattle
Boston
Items: 4, first: Austin, last: Boston
PowerShell split a string by comma with -split
Splitting a comma-separated string (PowerShell 7)

PowerShell also has the .Split() string method. The two behave differently in a few important ways, and one of them changed between PowerShell versions. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1.

Split by comma, semicolon, space, tab or several delimiters

"Semicolon: $(('alice@contoso.com;bob@contoso.com' -split ';') -join ' | ')"
"Tab:       $(("ID`tName`tState" -split "`t") -join ' | ')"
"Any of ,;: $(('Austin,Denver;Seattle' -split '[,;]') -join ' | ')"
"Spaces:    $((-split '  Austin   Denver  Seattle ') -join ' | ')"

Output:

Semicolon: alice@contoso.com | bob@contoso.com
Tab:       ID | Name | State
Any of ,;: Austin | Denver | Seattle
Spaces:    Austin | Denver | Seattle
Split byCode
Comma$text -split ','
Semicolon$text -split ';'
Tab$text -split "`t"
Any run of spaces-split $text (no left side)
Comma or semicolon$text -split '[,;]'

With nothing on its left, -split splits on any whitespace and drops the empty pieces, which is handy for text with uneven spacing.

To grab one piece of the result, see getting the second element after a split.

Why does splitting by a dot return empty strings?

-split treats the delimiter as a regular expression, and a dot matches any character. Escape it:

$ip = '192.168.1.10'

"-split '.':  $(($ip -split '.').Count) items, all empty"
"-split '\.': $(($ip -split '\.') -join ' | ')"
"Escape():    $(($ip -split [regex]::Escape('.')) -join ' | ')"

Output:

-split '.':  13 items, all empty
-split '\.': 192 | 168 | 1 | 10
Escape():    192 | 168 | 1 | 10
PowerShell -split by dot returns empty strings until you escape it
A plain dot splits on every character (PowerShell 7)

The same goes for |, $, +, ( and other regex characters. When you’re not sure, wrap the delimiter in [regex]::Escape(). Microsoft covers every option in about_Split.

.Split() vs -split: the Windows PowerShell 5.1 trap

The .Split() method doesn’t use regex, but it has its own catch. Here’s the same script in both versions:

$names = 'Alice Johnson; Bob Smith; Carol Davis'
".Split('; ')  -> $($names.Split('; ').Count) items: $($names.Split('; ') -join ' | ')"
"-split '; '   -> $(($names -split '; ').Count) items: $(($names -split '; ') -join ' | ')"

$lines = "Started`r`nFinished"
".Split(CRLF)  -> $($lines.Split("`r`n").Count) items"

Output in PowerShell 7:

.Split('; ')  -> 3 items: Alice Johnson | Bob Smith | Carol Davis
-split '; '   -> 3 items: Alice Johnson | Bob Smith | Carol Davis
.Split(CRLF)  -> 2 items

Output in Windows PowerShell 5.1:

.Split('; ')  -> 8 items: Alice | Johnson |  | Bob | Smith |  | Carol | Davis
-split '; '   -> 3 items: Alice Johnson | Bob Smith | Carol Davis
.Split(CRLF)  -> 3 items