How to Split a String by Semicolon in PowerShell (With Practical Examples)

Recently, I was working on a data migration project where I needed to process a large CSV file containing semicolon-delimited values in several columns. I quickly realized that knowing how to split strings by semicolons in PowerShell would be very useful for this.

PowerShell offers multiple methods to split strings by delimiters like semicolons.

In this tutorial, I will explain several practical ways to split strings by semicolons in PowerShell based on my decade of experience. Let’s dive in!

Split a String by Semicolon in PowerShell

Before we get into the specific methods, it’s important to understand what string splitting actually does.

When you split a string by a semicolon, PowerShell divides the original text at each semicolon character and returns an array of substrings. This is extremely useful when working with data that uses semicolons as separators.

Below are the methods to split a string by semicolon in PowerShell.

Method 1: Using the -Split Operator

The -Split operator is one of the most common and straightforward ways to split a string in PowerShell. Here is the complete script to split a string by semicolon using PowerShell.

$emailList = "john.doe@example.com;jane.smith@example.com;robert.johnson@example.com"
$emails = $emailList -split ";"
$emails

When you run this code, PowerShell will output:

john.doe@example.com
jane.smith@example.com
robert.johnson@example.com

The -Split operator is case-sensitive by default. If you need case-insensitive splitting, you can use the -isplit operator instead.

You can see the exact output in the screenshot below:

Split a String by Semicolon in PowerShell

Check out How to Split String by Space in PowerShell

Method 2: Using the String.Split() Method

Another common approach is to use the .Split() method, which is part of the String class in .NET. You can use the String.Split() method to split a string by using a semicolon.

$statesList = "New York;California;Texas;Florida;Illinois"
$states = $statesList.Split(";")
$states

This will give you an array containing:

New York
California
Texas
Florida
Illinois

The .Split() method is slightly more flexible as it allows you to split by multiple delimiters simultaneously:

$mixedData = "New York;California,Texas:Florida;Illinois"
$mixedStates = $mixedData.Split(";", ",", ":")
$mixedStates

Read Split a String and Get the Second Element in PowerShell

Method 3: Using the Split() Method with Options

If you need more control over the splitting process, you can use additional parameters with the Split() method. Here is an example.

$addressData = "123 Main St;;New York;NY;10001"
$addressParts = $addressData.Split(";", [StringSplitOptions]::RemoveEmptyEntries)
$addressParts

The output will be:

123 Main St
New York
NY
10001

Notice that the empty entry between “123 Main St” and “New York” is removed because we used the RemoveEmptyEntries option.

You can see the exact output in the screenshot below:

powershell split string by semicolon

Read PowerShell Split String by Comma

Method 4: Splitting and Processing in a Pipeline

PowerShell’s pipeline functionality allows you to split strings and process each part in one elegant command:

$productList = "Laptop;Smartphone;Tablet;Headphones;Keyboard"
$productList -split ";" | ForEach-Object { "Product: $_" }

This produces:

Product: Laptop
Product: Smartphone
Product: Tablet
Product: Headphones
Product: Keyboard

This approach is particularly useful when you need to perform operations on each element after splitting.

You can see the exact output in the screenshot below:

powershell split string by semicolon example

Check out How to Split Strings by Newlines in PowerShell?

Real-World Example: Process CSV Data with Semicolon Delimiters

Let’s look at a practical example. Suppose you have a CSV file with semicolon delimiters (common in European data formats) and need to process it:

$csvContent = @"
Name;Department;Position;Salary
John Smith;Marketing;Director;120000
Sarah Johnson;IT;Developer;95000
Michael Brown;Finance;Analyst;85000
"@

$csvContent -split "`n" | ForEach-Object {
    $rowData = $_ -split ";"

    # Skip the header row
    if ($rowData[0] -ne "Name") {
        [PSCustomObject]@{
            Name = $rowData[0]
            Department = $rowData[1]
            Position = $rowData[2]
            Salary = [int]$rowData[3]
        }
    }
} | Sort-Object -Property Salary -Descending

This script:

  1. Splits the CSV content by newlines
  2. For each line, splits by semicolons
  3. Creates custom objects for each employee (skipping the header)
  4. Sorts employees by salary in descending order

The output would be:

Name           Department Position  Salary
----           ---------- --------  ------
John Smith     Marketing  Director  120000
Sarah Johnson  IT         Developer  95000
Michael Brown  Finance    Analyst    85000

Read How to Split a String by Word in PowerShell?

Method 5: Using ConvertFrom-Csv with Custom Delimiter

If you’re specifically working with CSV-like data, PowerShell has a built-in cmdlet that can handle custom delimiters:

$semicolonCSV = @"
Name;Age;City
John Doe;32;New York
Jane Smith;28;Los Angeles
Robert Johnson;45;Chicago
"@

$people = $semicolonCSV | ConvertFrom-Csv -Delimiter ";"
$people

This outputs a table of objects with properties for each column:

Name           Age City
----           --- ----
John Doe       32  New York
Jane Smith     28  Los Angeles
Robert Johnson 45  Chicago

Here is the output in the screenshot below:

How to Split a String by Semicolon in PowerShell

You can then access individual properties:

$people | ForEach-Object { "$($_.Name) lives in $($_.City)" }

Method 6: Splitting with Regular Expressions

For more complex splitting requirements, you can use regular expressions with the -split operator:

$complexData = "value1;value2;;value3; value4;  value5"
$parts = $complexData -split ";+" | Where-Object { $_ -match "\S" }
$parts

This splits by one or more semicolons and filters out entries that are just whitespace, giving you:

value1
value2
value3
value4
value5

Check out Split a String into Variables in PowerShell

Common Problems and Solutions

Now, let me show you some common problems and solutions that you may face while splitting a string using a semicolon in PowerShell.

Handle Empty Elements

When splitting strings, you might encounter empty elements if there are consecutive delimiters:

$data = "item1;;item2;item3"
$items = $data -split ";"
$items.Count  # Returns 4
$items        # Shows an empty string at index 1

To handle this, you can either:

  1. Use RemoveEmptyEntries as shown earlier
  2. Filter the results after splitting:
$items = $data -split ";" | Where-Object { $_ -ne "" }

Deal with Semicolons in Content

If your data might contain semicolons as part of the content (not just as delimiters), you’ll need more sophisticated parsing. Consider this example with quoted values:

$quotedData = 'value1;"value2;with;semicolons";value3'

# Simple splitting won't work correctly here
$wrongSplit = $quotedData -split ";"

# A more complex approach is needed
$pattern = '(?<=^|;)(?:"([^"]*)"|((?:[^;"]|"[^"]*")+))(?=;|$)'
$correctSplit = [regex]::Matches($quotedData, $pattern) | ForEach-Object { 
    if ($_.Groups[1].Success) { $_.Groups[1].Value } 
    else { $_.Groups[2].Value }
}

$correctSplit

Check out Split String by Tab Character in PowerShell

Practical Applications and Performance Considerations

As a PowerShell developer, it is essential to understand the performance considerations. All these are based on my experience working with this.

When to Use Each Method

  • Use -split for simple, readable code in most scenarios
  • Use .Split() when you need to split by multiple delimiters
  • Use ConvertFrom-Csv for tabular data that follows CSV conventions
  • Use regex-based splitting for complex data with special requirements

Performance Comparison

For large datasets, performance can matter. Here’s a quick comparison:

$largeString = ("item1;item2;item3;" * 10000)

$time1 = Measure-Command { $largeString -split ";" }
$time2 = Measure-Command { $largeString.Split(";") }

"Split operator: $($time1.TotalMilliseconds) ms"
"Split method: $($time2.TotalMilliseconds) ms"

In my testing, the .Split() method is typically slightly faster for very large strings, but the difference is negligible for most everyday use cases.

Table: Method Comparison

MethodProsConsBest For
-split operatorSimple syntax, PowerShell nativeLess flexibility with optionsEveryday scripting
.Split() methodMore options, multiple delimiters.NET syntax less familiar to someComplex splitting requirements
ConvertFrom-CsvCreates structured objects automaticallyOnly works with CSV-like dataProcessing tabular data
Regex splittingHandles complex patternsMore difficult to read/maintainSpecial cases with complex patterns

In this tutorial, I explained how to split a string by semicolon in PowerShell using different methods. I hope you found this article helpful. If you have any questions or suggestions, please leave them in the comments below.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.