Skip to main content
SysAdmin Shell Scripting Essentials

powershell string split: -split vs .Split() CLI reference

powershell string split is the operator and method in PowerShell to split one or more strings into substrings based on a delimiter, regex pattern, or script block.

$string = "The best PowerShell blog" $string -split " " # Result The best PowerShell blog

Syntax

# Binary -split operator
-Split <String[]> [-Delimiter <String>] [-MaxSubstrings <Int32>] [-ScriptBlock <ScriptBlock>] [-Options <SplitOptions>]

# Unary -split operator (splits on whitespace)
-Split <String>

# .Split() method (string)
$string.Split($delimiter[, $count, $options])

Options and Flags

Parameter Type Default Description
-Delimiter String Whitespace One or more characters to split on. Supports regex when using -split.
-MaxSubstrings Int32 All substrings Maximum number of substrings returned. Negative values count from the end (PowerShell 7+).
-ScriptBlock ScriptBlock N/A Expression evaluated per substring to determine splitting (advanced).
-Options SplitOptions None Use SimpleMatch for literal delimiter, or RegexMatch (default) with IgnoreCase.
.Split() count Int32 All Number of substrings to return; 0 returns original string.
.Split() options System.StringSplitOptions None RemoveEmptyEntries or TrimEntries (.NET).
See also  PowerShell Escape Double Quote: Syntax and Troubleshooting

Usage Examples

Split a CSV line into array

$string = "apple,banana,cherry,date"
$string.split(',').trim()
# Output: apple banana cherry date

Extract last segment from hyphen-delimited string

$string = "Delivery-contract-20230401"
$splitArray = $string.Split("-")
$splitArray[-1]  # Result: 20230401

Split on multiple delimiters with regex

$string = "Hello123World456Test789"
$string -split "d+"  # Result: Hello World Test

Limit substrings to three fragments

'Chocolate-Vanilla-Strawberry-Blueberry' -split '(-)', 3
# Result: Chocolate - Vanilla - Strawberry-Blueberry

Split on multiple spaces or tabs

$string = "This is a string with multiple   spaces and tabs"
$string -split "s+"

Split on sentences

$string = "This is a sentence. And here is another one! Finally, a third sentence."
$string -split "[.!?]"

Frequently Asked Questions

What is the difference between the -split operator and the .Split() method in PowerShell?

Answer: The -split operator uses regex delimiters by default; .Split() treats the delimiter literally, without regex interpretation. For example, "a b c" -split 's+' splits on one or more spaces, while "a,b,c".Split(',') splits on commas only.

When should I use the -SimpleMatch flag with -split?

Answer: Use -SimpleMatch to force literal delimiter parsing, avoiding regex special characters. For example, "file.name.log" -split '.' -SimpleMatch splits on literal dots, not the regex wildcard.

How do I fix “Cannot index into a null array” when splitting an empty string?

Answer: Splitting a null or empty string with -split returns $null, not an empty array. Use the null-conditional operator (PowerShell 7+): $parts = $string?.Split(',') ?? @(). For PowerShell 5.1, wrap with an if block.

What is the fastest way to split a large string in PowerShell?

Answer: For large strings, the -split operator is generally faster than .Split() when using a simple regex delimiter. Use Measure-Command to benchmark your specific scenario.