Home » PowerShell Split and Join Operators

PowerShell Split and Join Operators

by Online Tutorials Library

Split and Join Operators

The Split and Join operators are used in PowerShell to divide and combine the substrings.

-Join Operator

The -Join operator is used in PowerShell to combine the set of strings into a single string. The strings are combined in the same order in which they appear in the command.

The following two statements are the syntax to use the Join operator:

In the above syntax, the <String> parameter is used to specify the one or more strings to be joined. And the <Delimiter> parameter is used to specify one or more characters placed between the combined strings. The default Delimiter is ” “.

Note: If we use the unary join operator (-join <String>) to combine the strings, we must enclose them in parenthesis, or store them in a variable.

Examples: The below examples describe how to use the unary and binary (With Delimiter) Join operator in different ways:

Example1:

The command in this example displays the following output:

Windows   Operating   System   

Example2:

The command in this example displays the following output:

WindowsOperatingSystem  

Example3:

The second command in this example displays the following output:

WindowsOperatingSystem   

Example4:

This example uses the multiple-character delimiter to join the three strings, which are stored in the variable $x. The second command in this example displays the following output:

WINDOWS POWERSHELL  

-Split Operator

The -Split operator is used in PowerShell to divide the one or more strings into the substrings.

The following statements are the syntax to use the -split operator:

In the above Syntax, the following parameters are used:

  1. <string> : This parameter is used to specify the one or more string to be split. The same delimiter rule splits the multiple strings.
  2. <delimiter> : The default delimiter is ” “. When the strings are split, it is omitted from all the substrings.
  3. <max-substrings> : This parameter is used to specify the maximum number of times that a string splits.
  4. <ScriptBlock> : This parameter is an expression that specifies the rules for applying the delimiter. We must enclose the script block in the braces”{}”.
  5. <Options> : This parameter is valid only when the <max-substring> parameter is used in the statement.

Examples: The following examples describe how to use the -split operator in different ways:

Example 1:

This command displays the following output:

a  b  c  d  e  f  g  h  

Example2:

The output of this example is the same as the output of example1.

Example3:

The output of this example is also same as the output of example1.

Example4:

This example displays the following output:

a  b  c=d=e=f=g=h  

Example5:

This example displays the following output:

a=  =c=d=e=  =g=h  

Next TopicIf Statement

You may also like