PowerShell: String Operators

By Xah Lee. Date: . Last updated: .

Operator names are case-insensitive.

Join

-Join (str1, str2 )
Join strings. (Parenthesis required.)
(str1, str2 ) -Join Delimiter
Join strings with delimiter. delimiter is a string and can have multiple characters.
-join ("a", "b", "c")
# "abc"
("a", "b", "c") -join " "
# "a b c"

Note, string can also be joined by +.

"a"+"b"+"c"
# "abc"

There is also a Join-String cmdlet.

Split

-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
-iSplit
Case-insensitive. Same as -Split.
-cSplit
Case-sensitive.

String Match by Wildcards

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

# the wildcard must be on right side
-iLike
Case-insensitive. Same as -Like.
-cLike
Case-sensitive
-notLike
Negation.
-iNotLike
Case-insensitive. Same as -NotLike.
-cNotLike
Case-sensitive

String Match by Regex

-match
String matches regex pattern. Case-insensitive
"mycat.jpg" -match ".+jpg"
# True

# the regex must be on right side
-iMatch
Case-insensitive. Same as -Match.
-cMatch
Case-sensitive
-notmatch
Negation.
-iNotmatch
Case-insensitive. Same as -Notmatch.
-cNotmatch
Case-sensitive

String Replacement Operator

-Replace
Replaces strings matching a regex pattern. Case-Insensitive.
-iReplace
Case-insensitive. Same as -Replace.
-cReplace
Case-sensitive
"wonderland" -replace "wonder", "lala"
# result
# lalaland

PowerShell String

PowerShell

How-to

Advanced

Script Examples