PowerShell: String Operators

By Xah Lee. Date: . Last updated: .

Operator names are case-insensitive.

String Length


Join String

Join String +

string can be joined by plus sign +.

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

-Join operator

-Join (str1, str2 )
Join strings. (Parenthesis required.)
-join ("a", "b", "c")
# "abc"
(str1, str2 ) -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 )
Can have multiple operands.
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 regex.
• 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.
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.
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
Case-insensitive. Same as -Split.

String Match by Wildcards ( -like operator)

String matches wildcard pattern. Case-insensitive
"mycat.jpg" -like "*jpg"
# True

# the wildcard must be on right side
Case-insensitive. Same as -Like.
Case-insensitive. Same as -NotLike.

Regex Operators

PowerShell String

PowerShell Operators


Practical How-To

list files

list dirs

on dir

on file

Windows config


PowerShell in Depth

Programing PowerShell

Value Types





Data Structure

Loop and Iteration



Profile and Script

Script Examples