Recently, I was working on a project that required parsing a log file containing space-separated values. The challenge was to extract individual components from each line and process them separately. This is where PowerShell’s string splitting capabilities came to my rescue.
In this tutorial, I will explain different methods to split strings by spaces in PowerShell, with practical examples.
Split String by Space in PowerShell
I will show you now how to split a string by space in PowerShell using the methods below.
Method 1: Using the -Split Operator
The best way to split a string by space in PowerShell is using the -split operator. This is my go-to method for most scenarios.
Here’s a simple example:
$text = "John Smith lives in New York"
$result = $text -split " "
$resultOutput:
John
Smith
lives
in
New
YorkYou can see the exact output in the screenshot below:

What I love about the -split operator is its simplicity. It creates an array of substrings that you can easily access using index values:
$text = "John Smith lives in New York"
$result = $text -split " "
# Access individual elements
$firstName = $result[0] # John
$lastName = $result[1] # Smith
$city = $result[4] + " " + $result[5] # New York
Write-Host "First name: $firstName"
Write-Host "Last name: $lastName"
Write-Host "City: $city"Using Regular Expressions with -Split
One of the powerful features of the -split operator is that it accepts regular expressions. This comes in handy when dealing with multiple spaces or different whitespace characters:
$text = "John Smith lives in New York"
$result = $text -split "\s+"
$resultOutput:
John
Smith
lives
in
New
YorkThe \s+ regular expression matches one or more whitespace characters, which helps clean up messy data with inconsistent spacing.
Check out Split a String and Get the Second Element in PowerShell
Method 2: Using the String.Split() Method
Another approach is to use the Split() method that’s available for string objects in PowerShell. This is a method from the .NET framework that PowerShell inherits:
$text = "John Smith lives in New York"
$result = $text.Split(" ")
$resultThe output is the same as our first example. The Split() method gives you the same result but uses a different syntax that some developers find more intuitive, especially if they’re coming from other programming languages.
You can see the exact output in the screenshot below:

Split with Multiple Delimiters
What I find particularly useful about the Split() method is that it allows you to specify multiple delimiters:
$text = "John Smith;lives in,New York"
$result = $text.Split(" ", ";", ",")
$resultOutput:
John
Smith
lives
in
New
YorkThis is extremely helpful when parsing complex data formats where different characters might separate fields.
Limit the Number of Splits
Sometimes you only want to split a string into a specific number of parts. For example, if you’re parsing a log file where only the first few fields are relevant:
$text = "John Smith lives in New York"
$result = $text.Split(" ", 3)
$resultOutput:
John
Smith
lives in New YorkThis splits the string into exactly 3 parts, keeping the rest intact in the last element.
Read PowerShell Split String by Comma
Method 3: Split-String Advanced Function
While PowerShell doesn’t have a built-in Split-String cmdlet, I’ve found it useful to create a custom function for more complex splitting operations:
function Split-String {
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]$String,
[Parameter(Mandatory=$false)]
[string]$Delimiter = " ",
[Parameter(Mandatory=$false)]
[switch]$NoEmptyEntries
)
if ($NoEmptyEntries) {
return $String.Split($Delimiter, [System.StringSplitOptions]::RemoveEmptyEntries)
} else {
return $String.Split($Delimiter)
}
}This function provides a more PowerShell-like interface and can be used in pipelines:
$text = "John Smith lives in New York"
$text | Split-String -NoEmptyEntriesThe -NoEmptyEntries switch is particularly useful when dealing with strings that might have consecutive spaces.
You can see here that I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Check out How to Split Strings by Newlines in PowerShell?
Method 4: Split and Process in One Go
When working with large files or datasets, I often use a combination of splitting and processing in a single pipeline. This is more efficient than storing the entire split result in memory:
$text = "John Smith lives in New York"
$text -split " " | ForEach-Object {
# Process each word
"Processing: $_"
}Output:
Processing: John
Processing: Smith
Processing: lives
Processing: in
Processing: New
Processing: YorkThis approach is especially useful when working with large log files where you need to extract and process specific fields.
Read Split a String by Word in PowerShell
Practical Example: Parse Command Output
Let’s look at a real-world example. Say you want to extract IP addresses from the output of ipconfig:
$ipconfigOutput = ipconfig | Where-Object { $_ -match "IPv4 Address" }
$ipAddresses = $ipconfigOutput | ForEach-Object {
($_ -split ": ")[1]
}
Write-Host "Your IP addresses:"
$ipAddressesThis script extracts all IPv4 addresses from the ipconfig command output by splitting each line at the colon-space delimiter and taking the second part.
Check out Split a String into Variables in PowerShell
Common Problems and How to Fix Them
Now, let me show you some common problems and how to fix them.
1. Empty Entries in Results
When splitting strings with multiple consecutive spaces, you might end up with empty entries in your array:
$text = "John Smith"
$text -split " "Output:
John
SmithTo avoid this, use the regular expression approach:
$text = "John Smith"
$text -split "\s+"Output:
John
Smith2. Preserve Quotes
Sometimes your string might contain quoted text that you want to keep together:
$text = 'Command --param "New York"'
$text -split " "This would split “New York” into separate words. To handle this case, you need a more sophisticated approach:
function Split-PreservingQuotes {
param([string]$text)
$pattern = '(?<=^|\s)("(?:[^"]|"")*"|[^\s]*)(?=\s|$)'
[regex]::Matches($text, $pattern) | ForEach-Object { $_.Value }
}
$text = 'Command --param "New York"'
Split-PreservingQuotes $textOutput:
Command
--param
"New York"3. Performance Considerations
When working with very large strings, performance can become an issue. In my experience, the -split operator is generally faster than the .Split() method for simple cases, but when using complex regular expressions, the difference can vary.
For critical performance scenarios, I recommend testing both approaches with your specific data.
Check out Split a String and Get the First and Last Element in PowerShell
Summary Table: Various String Splitting Methods
| Method | Syntax | Best For | Limitations |
|---|---|---|---|
-split operator | $text -split " " | Simple splits, regex support | Less intuitive for multi-char delimiters |
.Split() method | $text.Split(" ") | Multiple delimiters, limiting splits | No direct regex support |
| Custom function | $text | Split-String | Pipeline operations, reusable code | Requires additional code |
| Pipeline processing | $text -split " " | ForEach-Object {...} | Processing large datasets | More verbose |
In this tutorial, I explained how to split a string by space in PowerShell using various methods with examples. I hope you found this tutorial helpful. If you have any questions or suggestions, kindly leave them in the comments below.
Other PowerShell tutorials you may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.