How to split a string content into an array of strings in PowerShell? How to split a string content into an array of strings in PowerShell? powershell powershell

How to split a string content into an array of strings in PowerShell?


As of PowerShell 2, simple:

$recipients = $addresses -split "; "

Note that the right hand side is actually a case-insensitive regular expression, not a simple match. Use csplit to force case-sensitivity. See about_Split for more details.


[string[]]$recipients = $address.Split('; ',[System.StringSplitOptions]::RemoveEmptyEntries)


Remove the spaces from the original string and split on semicolon

$address = "foo@bar.com; boo@bar.com; zoo@bar.com"$addresses = $address.replace(' ','').split(';')

Or all in one line:

$addresses = "foo@bar.com; boo@bar.com; zoo@bar.com".replace(' ','').split(';')

$addresses becomes:

@('foo@bar.com','boo@bar.com','zoo@bar.com')