Home » PowerShell » PowerShell Split by Multiple Characters

PowerShell Split by Multiple Characters

Using the PowerShell string built-in Split() method or Split operator, you can easily split the string by multiple characters.

In this article, we will discuss how to split a string by multiple characters as separators using the PowerShell string Split() method and split operator.

Use the Split() function to Split String by Multiple Characters

Use the PowerShell string Split() function to specify multiple characters in the Split() method.

$str = "Hello!Welcome your email address: [email protected]"
$str.Split(" !:@.")

In the above PowerShell script, the $str variable contains the string that contains multiple characters in the given string.

We have used the PowerShell string Split() function to split a string by multiple characters based on multiple characters as delimiters like !:@ etc…

The output of the above script to break the string into multiple strings by special characters as delimiters are:

PS C:\> $str = "Hello!Welcome your email address: [email protected]"                                                  

PS C:\> $str.Split(" !:@.")                                                                                             
Hello
Welcome
your
email
address

Use Split operator to Split String by Multiple Delimiters in PowerShell

You can use the split operator to split the string into multiple substrings based on the multiple delimiters provided.

$str = "Hello!Welcome your email address: [email protected]"
$str -split "!|:|@"    

In the above PowerShell script, the $str variable stores the string that contains multiple special characters.

To split a string by multiple delimiters in PowerShell, we have used the split operator. The split operator takes multiple delimiters as input and breaks the string into multiple substrings.

The output of the above PowerShell script to break the string by multiple characters is:

PS C:\> $str = "Hello!Welcome your email address: [email protected]" 

PS C:\> $str -split "!|:|@"                                                                                             
Hello
Welcome your email address
 admin
ShellGeek.com
PS C:\>   

Conclusion

I hope the above article on how to split the string by multiple characters is helpful to you.

You can use PowerShell string built-in Split() method or split operator to split a string inot multiple substrings.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.