PowerShell Split and Join Strings: Practical Guide

Parsing a log line, extracting a hostname from a URL, splitting a CSV field — these tasks come up constantly in PowerShell scripts, and they all rely on the same two operators. PowerShell split string operations use the -split operator to break a string apart, while -join reassembles pieces back into a single string. Together they make parsing delimited text fast and readable. This guide covers every practical form of both operators.
Quick Answer / TL;DR
# Split on a delimiter
'web01,web02,db01' -split ',' # Returns: 'web01', 'web02', 'db01'
# Join an array back into a string
@('web01','web02','db01') -join ',' # Returns: 'web01,web02,db01'
Basic -split Syntax
The -split operator treats its right-hand side as a regular expression delimiter and returns an array of substrings:
$csv = 'Alice,Engineering,New York,Senior'
# Split on comma
$fields = $csv -split ','
$fields
# Access individual parts
$fields[0] # Alice
$fields[1] # Engineering
$fields.Count
Alice
Engineering
New York
Senior
Alice
Engineering
4
The result is always an array, even if there’s only one element. When you need just a specific piece, index directly into the result: ($line -split ',')[2].
Split on Multiple Characters
Since -split uses regex, you can split on any pattern — including multiple possible delimiters using the regex alternation operator |:
$mixed = 'web01;web02,db01 cache01'
# Split on semicolon, comma, or space
$servers = $mixed -split '[;, ]'
$servers
web01
web02
db01
cache01
The character class [;, ] matches any one of those characters. This handles inconsistently formatted input gracefully.
Limiting the Number of Parts
Pass a third argument to limit how many substrings are returned. The last element contains the remainder of the string, unsplit:
$logLine = '2026-05-04 09:15:33 ERROR Database connection timeout on host db01'
# Split on space, but only into 4 parts (date, time, level, message)
$parts = $logLine -split ' ', 4
$parts[0] # 2026-05-04
$parts[1] # 09:15:33
$parts[2] # ERROR
$parts[3] # Database connection timeout on host db01
2026-05-04
09:15:33
ERROR
Database connection timeout on host db01
This is particularly useful for log parsing where the first N fields are structured but the message field can contain the delimiter character.
Using Regex with -split
Because -split is regex-based, you can use full regex patterns as delimiters:
# Split on one or more whitespace characters (handles multiple spaces/tabs)
'word1 word2`tword3 word4' -split '\s+'
# Split on digits (extract non-numeric parts)
'abc123def456ghi' -split '\d+'
# Case-insensitive split using the -isplit variant
'oneXtwoXthree' -split 'x' # case-insensitive by default
'oneXtwoXthree' -csplit 'x' # case-sensitive (only matches lowercase x)
word1
word2
word3
word4
abc
def
ghi
Joining Arrays with -join
The -join operator concatenates array elements with a separator between each element:
$servers = @('web01', 'web02', 'db01')
# Join with comma
$servers -join ','
# Join with pipe for a log-friendly format
$servers -join ' | '
# Join with no separator — concatenates directly
@('Hello', ' ', 'World') -join ''
# Build a comma-separated list for SQL IN clause
$ids = @(101, 102, 103, 104)
"WHERE id IN ($($ids -join ', '))"
web01,web02,db01
web01 | web02 | db01
Hello World
WHERE id IN (101, 102, 103, 104)
Real-World CSV Line Parsing
Combining -split with array indexing handles basic CSV line processing. For full CSV files with headers, use Import-Csv instead — but for one-off line parsing, this pattern is fast:
$csvLines = @(
'alice,Engineering,Senior,120000',
'bob,Marketing,Manager,95000',
'carol,Engineering,Lead,140000'
)
foreach ($line in $csvLines) {
$cols = $line -split ','
$name = $cols[0]
$dept = $cols[1]
$salary = [int]$cols[3]
if ($dept -eq 'Engineering' -and $salary -gt 100000) {
Write-Output "$name earns `$$salary in $dept"
}
}
alice earns $120000 in Engineering
carol earns $140000 in Engineering
Common Errors and Fixes
-
-split treats delimiter as regex by default:
'1.2.3' -split '.'returns empty strings everywhere because.matches any character. Escape the dot:'1.2.3' -split '\.'or use[regex]::Escape('.'). -
Split returns array — indexing needed for single value:
$host = '192.168.1.100' -split '\.'gives you a 4-element array. To get just the last octet, index it:$host[-1]. Assigning the whole split to a single variable gives you the array, not a scalar.
Related Cmdlets / See Also
Wrapping Up
Use -split to break strings apart on any pattern and -join to reassemble arrays into strings. Remember that -split interprets its delimiter as regex — escape literal special characters. Limit parts with the third argument when parsing structured log lines. Your next step: apply these techniques to parse a real log file from your environment.


