How to Split a String by Word in PowerShell?

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
$parts

In 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 
 country

I executed the above PowerShell script, and you can see the output in the screenshot below:

Split a String by Word in PowerShell

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
$parts

In this example, the string is split using the word "beautiful" as the delimiter. The output will be:

United States is a 
 country

After executing the code, you can see the output in the screenshot below:

How to Split a String by Word in PowerShell

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)"
$parts

In this example, the regular expression ($delimiter) is used to split the string by the word "beautiful". The output will be:

United States is a 
 country

Method 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 "\|"
$parts

In 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 
 country

Read 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
$after

This script uses a regex pattern to capture everything before and after the word "beautiful". The output will be:

United States is a 
 country

You can see the screenshot below:

powershell split string by word

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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