PowerTip: Split String with PowerShell


Summary: Easily split a string on multiple characters with Windows PowerShell.

Hey, Scripting Guy! Question How can I use Windows PowerShell to split a string that contains multiple separator characters (for example, a comma and a semicolon)?

Hey, Scripting Guy! Answer Specify a string with the characters in it to the Split method:

PS C:\> $string = "This,is;a;string"

PS C:\> $string.Split(',;')

This

is

a

string

Comments (11)

  1. JVimes says:

    @Ben, the easiest way to do a multi-character delimiter is to use the -split operator.
    Example: "This,is,;a;string" -split ‘,;’

  2. Chen V says:

    We can get output like ‘This is a String’ using the below code
    ‘This;is,a;String’.Split(‘;,’) -join ‘ ‘

  3. Also didn’t know that, also performed two splits… thanks!

  4. Bjorn Houben says:

    Didn’t know that … I generally just performed 2 splits.

  5. Ben says:

    Is there a way to split on double characters? eg., "This,;is,;a,;string" ?

  6. James Brown says:

    This is a nice way to separate your Windows path into readable output:

    $path = $env:path
    $path.split(";")

    @Ben, just put your double characters inside the quotes:
    $string = "This,;is,;a,;string"
    $string.split(‘,;’)

  7. Rocky says:

    What if the separator character is tick ‘? Are there any escape characters?

  8. Rocky says:

    What if the separator character is tick ‘? Are there any escape characters?

  9. gpm1982 says:

    @Chen V, that’s a cool trick! Normally I would use -Replace multiple times, this really comes handy. Thanks!

  10. Geoff says:

    Too simple, split can take args…! Return me the 3rd field after split…

Skip to main content