PowerTip: Split String with PowerShell

PowerTip: Split String with PowerShell

  • Comments 9
  • Likes

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Didn't know that ... I generally just performed 2 splits.

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

  • Also didn't know that, also performed two splits... thanks!

  • 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(',;')

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

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

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

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

  • thank you