Today, I thought I would do a different example of split strings in PowerShell. In this tutorial, I will show you how to split a string by word in PowerShell with a complete script and examples.
Split a String by Word in PowerShell
Now, let us check various methods to split a string by word in PowerShell.
Method 1: Using the -split Operator with a Specific Word
The -split operator in PowerShell can be used with a specific word as the delimiter.
Here is a complete example:
$string = "United States is a beautiful country"
$delimiter = "beautiful"
$parts = $string -split $delimiter
$partsIn this example, the string "United States is a beautiful country" is split into an array using the word "beautiful" as the delimiter. The output will be:
United States is a
countryI executed the above PowerShell script, and you can see the output in the screenshot below:

Method 2: Using the .Split() Method with a Specific Word
The .Split() method in .NET can also be used to split a string by a specific word in PowerShell. However, this method requires converting the string into a character array.
Here is an example:
$string = "United States is a beautiful country"
$delimiter = "beautiful"
$parts = $string -split $delimiter
$partsIn this example, the string is split using the word "beautiful" as the delimiter. The output will be:
United States is a
countryAfter executing the code, you can see the output in the screenshot below:

Read How to Split Strings by Newlines in PowerShell?
Method 3: Using Regular Expressions with -split
PowerShell’s -split operator can work with regular expressions to split a string by word. This can be useful if the delimiter word appears in different forms (e.g., case-insensitive).
Example:
$string = "United States is a beautiful country"
$delimiter = "beautiful"
$parts = $string -split "($delimiter)"
$partsIn this example, the regular expression ($delimiter) is used to split the string by the word "beautiful". The output will be:
United States is a
countryMethod 4: Using -replace and -split
In PowerShell, you can also use the -replace operator to replace the specific word with a unique character or string, and then use the -split operator.
Example:
$string = "United States is a beautiful country"
$delimiter = "beautiful"
$string = $string -replace $delimiter, "|"
$parts = $string -split "\|"
$partsIn this example, the word "beautiful" is replaced with the pipe character |, and then the string is split by |. The output will be:
United States is a
countryRead Add Double Quotes in a String in PowerShell
Method 5: Using Select-String and -match
You can also use Select-String and -match to extract parts of the string before and after the specific word in PowerShell.
Example:
$string = "United States is a beautiful country"
$delimiter = "beautiful"
if ($string -match "(.*)$delimiter(.*)") {
$before = $matches[1]
$after = $matches[2]
}
$before
$afterThis script uses a regex pattern to capture everything before and after the word "beautiful". The output will be:
United States is a
countryYou can see the screenshot below:

Conclusion
In this PowerShell tutorial, I have explained how to split a string by word in PowerShell using various methods like:
- Using the -split Operator with a Specific Word
- Using the .Split() Method with a Specific Word
- Using Regular Expressions with -split
- Using -replace and -split
- Using Select-String and -match
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.