PowerShell: String Operators

By Xah Lee. Date: . Last updated: .

Operator names are case-insensitive.

String Length

"abc".length

Join String

Join String +

string can be joined by plus sign +.

# join string
$x = "a" + "b"
"a"+"b"+"c"
# "abc"

-Join operator

-Join (str1, str2 etc)

Join strings. (Parenthesis required.)

-join ("a", "b", "c")
# "abc"
(str1, str2 etc) -Join Delimiter

Join strings with delimiter. delimiter is a string and can have multiple characters.

("a", "b", "c") -join " "
# "a b c"

Join-String cmdlet

There is also a Join-String cmdlet.

-Split Operator

-Split str

Split string by whitespace such as `n, `t.

Return a Array .

-split "cat dog"
-Split (str1, str2 etc)

split multiple strings.

-Split ("a b", "c d")

<#
a
b
c
d
#>
str -Split delimiter
  • Use a delimiter.
  • delimiter is a string, can be multiple characters.
  • by default, delimiter is case-insensitive.
  • by default, delimiter is interpreted as Regular Expression.
  • The delimiters are removed from string. To include them in string, add parenthesis around it. For example: "(,)" splits string by comma, but include it in the result array.
"a,b,c" -Split ","

<#
a
b
c
#>
"a,b,c" -Split "(,)"

<#
a
,
b
,
c
#>
str -Split delimiter, max
  • Split up to max count.
  • Rest of string is concatenated as last element in array.
  • Note, max is not the result length.
  • Also, when including delimiter in result, it does not count towards max count.
"a b c" -Split " " , 1

<#
a b c
#>


"a b c" -Split " " , 2

<#
a
b c
#>

"a b c" -Split " " , 3

<#
a
b
c
#>
str -Split delimiter, max, options

Options is one of:

  • "SimpleMatch", "SimpleMatch,IgnoreCase"
  • Comma separated string of one or more of RegexMatch, IgnoreCase,CultureInvariant,IgnorePatternWhitespace,ExplicitCapture
  • Comma separated string of one of Singleline,Multiline
str -Split {ScriptBlock}

Use a script block.

str -Split {ScriptBlock}, Max

Use a script block with Max

-iSplit

Case-insensitive. Same as -Split.

-cSplit

Case-sensitive.

String Match by Wildcards ( -like operator)

Regex Operators

PowerShell String

PowerShell Operators