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.

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
PowerShell .Split() splits on each character in Windows PowerShell 5.1
.Split() with a multi-character delimiter in Windows PowerShell 5.1

In 5.1, .Split('; ') splits on the semicolon and on every space, so first and last names end up apart. It also leaves an empty line when you split on "`r`n".

-split gives the same result in both versions, so I use it in scripts that might run on either.

Split a string into variables

Assign the result to several variables at once. The last variable gets everything that’s left:

$first, $last = 'Alice Johnson' -split ' '
$user, $domain = 'alice@contoso.com' -split '@'
$head, $rest = 'Austin,Denver,Seattle,Boston' -split ','

"First name: $first, last name: $last"
"User: $user, domain: $domain"
"Head: $head, rest: $($rest -join ', ')"

Output:

First name: Alice, last name: Johnson
User: alice, domain: contoso.com
Head: Austin, rest: Denver, Seattle, Boston

Get the first or last element

Index the result: [0] is the first item and [-1] is the last. Note the doubled backslash, since a single one is a regex escape character:

$path = 'C:\Reports\2026\Q3\sales-summary.xlsx'
$parts = $path -split '\\'

"Drive: $($parts[0])"
"File:  $($parts[-1])"
"Year:  $($parts[2])"

Output:

Drive: C:
File:  sales-summary.xlsx
Year:  2026

For file paths, Split-Path is often clearer. See how to get the path of a file.

Limit the number of pieces

A number after the delimiter sets the maximum number of pieces. The last piece keeps the rest of the string, delimiters included:

$log = 'ERROR: Disk full: C: has 0 GB free'

($log -split ': ', 2)[1]

Output:

Disk full: C: has 0 GB free

That’s useful for log lines and settings where the value itself might contain the delimiter.

Split by a word, ignore or match case, keep the delimiter

"By word:     $(('Austin and Denver or Seattle' -split ' and | or ') -join ' | ')"
"-split AND:  $(('fooANDbarandbaz' -split 'AND') -join ' | ')"
"-csplit AND: $(('fooANDbarandbaz' -csplit 'AND') -join ' | ')"
"Keep delim:  $(('A1B2C3' -split '(\d)' | Where-Object { $_ }) -join ' | ')"

Output:

By word:     Austin | Denver | Seattle
-split AND:  foo | bar | baz
-csplit AND: foo | barandbaz
Keep delim:  A | 1 | B | 2 | C | 3

-split ignores case, and -csplit matches it exactly. Putting the delimiter in parentheses keeps it in the result.

Split a multiline string into an array of lines

Split on "`r?`n" to handle both Windows and Linux line endings, then drop the empty last line:

$text = "Alice Johnson`r`nBob Smith`nCarol Davis`r`n"

$names = $text -split "`r?`n" | Where-Object { $_ }
"Lines: $($names.Count)"
$names

Output:

Lines: 3
Alice Johnson
Bob Smith
Carol Davis

For files, Get-Content already returns one line per item, so you don’t need to split at all.

Remove empty entries and extra spaces

Double delimiters and trailing commas leave empty items. Filter them out and trim each piece:

$tags = ' sales , west ,, priority , '

"Where-Object: $((($tags -split ',').Trim() | Where-Object { $_ }) -join ' | ')"
"RemoveEmptyEntries: $($tags.Split([char[]]',', [StringSplitOptions]::RemoveEmptyEntries).Count) pieces"

Output:

Where-Object: sales | west | priority
RemoveEmptyEntries: 4 pieces

RemoveEmptyEntries found 4 pieces, not 3. It only drops pieces that are completely empty, and the space after the last comma isn’t. That’s why I trim first and filter after.

PowerShell 7 can do both in one call with the TrimEntries option:

' sales , west ,, priority , '.Split(',', [StringSplitOptions]'RemoveEmptyEntries, TrimEntries')

Output in PowerShell 7:

sales
west
priority

TrimEntries doesn’t exist in Windows PowerShell 5.1, so the script stops with an error there. Use the Where-Object version for 5.1.

Don’t split CSV lines on commas

A value in quotes can contain a comma, and -split doesn’t know that. ConvertFrom-Csv does:

$line = 'Alice Johnson,"Austin, TX",Admin'

"-split ',':      $(($line -split ',').Count) parts"
$row = $line | ConvertFrom-Csv -Header Name, City, Role
"ConvertFrom-Csv: $($row.Name) | $($row.City) | $($row.Role)"

Output:

-split ',':      4 parts
ConvertFrom-Csv: Alice Johnson | Austin, TX | Admin

For whole files, use Import-Csv instead of splitting lines yourself.

Frequently Asked Questions

How do I split a string in PowerShell?

Use $text -split ','. It returns an array of the pieces. Replace the comma with any delimiter, like ';' or "`t".

What’s the difference between -split and .Split() in PowerShell?

-split uses regular expressions and works the same in 7 and 5.1. .Split() doesn’t use regex, but in 5.1 it splits on each character of a multi-character delimiter.

How do I split a string by a dot in PowerShell?

Escape it: $text -split '\.'. An unescaped dot is a regex wildcard, so every character becomes a delimiter.

How do I split a string into lines in PowerShell?

Use $text -split "`r?`n". It works for both Windows (CRLF) and Linux (LF) line endings.

How do I get the last element after a split?

Use index -1: ($text -split ',')[-1]. Use [0] for the first element.

These string guides pair well with splitting: