You can use the following basic syntax to extract text between two strings using PowerShell:
[regex]::Match($original_string, '[^string1]+(?=string2)').Value
This particular example extracts the text between string1 and string2 within the string variable named original_string.
The following example shows how to use this syntax in practice.
Example: How to Extract Text Between Two Strings in PowerShell
Suppose that we have the following string:
- His ID Number:004593Employee
And suppose that we would like to extract only the text between the colon ( : ) and the string Employee to get the following value:
- 004593
We can use the following syntax to do so:
$original_string = "His ID Number:004593Employee"
[regex]::Match($original_string, '[^:]+(?=Employee)').Value
The following screenshot shows how to use this syntax in practice:

Notice that we’re able to extract only the 004593 from the string, which represents all of the text between the colon ( : ) and Employee.
If you would like to extract the text between specific strings several times then you can use the Matches method instead.
For example, suppose that we have the following string:
- The ID Values are #004593Employee and #002321Employee
And suppose that we would like to extract only the number for each employee ID to get the following:
- 004593
- 002321
We can use the following syntax to do so:
$original_string = "The ID Values are #004593Employee and #002321Employee"
[regex]::Matches($original_string, '[^#]+(?=Employee)').Value
The following screenshot shows how to use this syntax in practice:

Notice that we’re able to extract only the number for each employee ID to get the following:
- 004593
- 002321
By using the Matches method, we’re able to extract all occurrences of text between two specific strings.
Related Tutorials
The following tutorials explain how to perform other common tasks in PowerShell:
PowerShell: How to Replace Every Occurrence of String in File
PowerShell: How to Replace Text with Wildcard
PowerShell: How to Replace Special Characters in String
