Today, I will show you a few interesting examples. In this tutorial, I will show you how to extract text from strings between delimiters using PowerShell.
To extract text between delimiters using PowerShell, you can use several methods, including the -split operator, regular expressions with Select-String, Substring and IndexOf methods, or custom functions. For example, using the -split operator, you can split a string like "California:CA; Texas:TX" by '; ' and then by ':' to isolate the state names.
PowerShell provides different methods to achieve this. Let me show you one by one with examples.
Method 1: Using -split Operator
The -split operator in PowerShell is the best way to split strings based on delimiters. Consider a scenario where you have a string containing a list of states and their abbreviations, and you want to extract the state names.
Here is an example.
$string = "California:CA; Texas:TX; New York:NY; Florida:FL"
$states = $string -split '; '
foreach ($state in $states) {
$name = ($state -split ':')[0]
Write-Output $name
}In this example, the string is first split by the semicolon and space delimiter (;) to separate each state. Then, each state is further split by the colon (:) to extract the state name.
You can see the output in the screenshot below after I executed the above PowerShell script.

Read Replace Special Characters in a String in PowerShell
Method 2: Using Regular Expressions with Select-String
Regular expressions (regex) provide a powerful way to match patterns within strings. The Select-String cmdlet can be used to apply regex in PowerShell.
Let me show you an example to understand it better.
Imagine you have a log file containing entries for various cities and their population, formatted as City[Population]. You need to extract the city names.
$log = @"
New York[8419600]
Los Angeles[3980400]
Chicago[2716000]
Houston[2328000]
"@
$regex = '\[(.*?)\]'
$matches = $log | Select-String -Pattern $regex -AllMatches
foreach ($match in $matches.Matches) {
$city = $match.Line -replace $regex, ''
Write-Output $city
}Here, the regex pattern '\[(.*?)\]' is used to match the population within square brackets. The -replace operator then removes the population, leaving only the city names.
Method 3: Using Substring and IndexOf Methods
To extract text from strings between delimiters using PowerShell, you can also use the Substring and IndexOf methods. This method is useful when dealing with fixed or predictable formats.
Here is an example.
Suppose you have a string containing dates in the format MM/DD/YYYY and you want to extract just the month.
$dateString = "04/15/2025"
$delimiter = "/"
$firstDelimiterIndex = $dateString.IndexOf($delimiter)
$month = $dateString.Substring(0, $firstDelimiterIndex)
Write-Output $monthIn this example, IndexOf finds the position of the first delimiter (/), and Substring extracts the text from the beginning of the string up to the first delimiter.
You can see the output in the screenshot below:

Check out Extract Strings Between Parentheses in PowerShell
Method 4: Using Custom Function with Regular Expressions
You can also create a custom function and reuse it. Let’s create a function to extract text between two delimiters.
Here is an example.
Suppose you have a string containing addresses in the format Street, City, State, ZIP and you want to extract the city names.
function Get-TextBetween {
param (
[string]$inputString,
[string]$startDelimiter,
[string]$endDelimiter
)
$pattern = [regex]::Escape($startDelimiter) + '(.*?)' + [regex]::Escape($endDelimiter)
if ($inputString -match $pattern) {
return $matches[1]
}
return $null
}
$addressString = "123 Main St, Springfield, IL, 62701; 456 Elm St, Chicago, IL, 60601"
$addresses = $addressString -split '; '
foreach ($address in $addresses) {
$city = Get-TextBetween -inputString $address -startDelimiter ", " -endDelimiter ", IL"
Write-Output $city
}In this example, the Get-TextBetween function takes an input string and two delimiters, using regex to extract the text between them. The addresses are split by ;, and the city names are extracted by specifying the appropriate delimiters.
Conclusion
PowerShell provides multiple methods to extract text between delimiters, such as the -split operator, regular expressions with Select-String, Substring and IndexOf methods, or custom functions, etc.
In this tutorial, I have explained how to extract text from strings between delimiters using PowerShell.
You may also like the following tutorials:
- Extract Lines Between Two Strings in PowerShell
- Replace Multiple Strings in a String Using PowerShell
- Replace a Character in a String at a Specific Position in PowerShell
- PowerShell String Comparison: Case-Sensitive vs Case-Insensitive
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.