How to concatenate strings in Powershell?

For this Powershell tutorial, we look at ways by which we can concatenate strings in Powershell. There are multiple ways by which you can join strings in Powershell. The simplest is to read the strings in variables and use variables to concatenate the strings. We look at some simple methods to concatenate strings in Powershell below.

Below, we take a look at five different ways by which you can concatenate strings in Powershell. Each method will use the Write-output cmdlet of Powershell to display the results of a concatenated string or the final string.

Concatenate strings in Powershell using ‘+’ operator

The simplest and quickest method to concatenate two strings in Powershell is through the ‘+’ operator. You can define strings in Powershell and combine all these using the ‘+’ operator. Use the write-output command to display the concatenated string on the screen. Or, you could redirect the output to a static text file. This ensures that the concatenated string is saved in a flat file structure and can be used anytime in the future.

For the purpose of an example that concatenates two or more strings using the ‘+’ operator, we will use the following process:

  • Define 3 strings and store them in variables $str1, $str2, and $str3.
  • Use the ‘+’ operator to concatenate $str1, $str2, and $str 3. Store the result in a variable $concat1.
  • Use the write-output cmdlet to display the concatenated string on the screen.

The command output for this process is shared below for your ready reference. This command output can also be redirected to a file instead of the display screen.

Concatenate strings using join operator in Powershell

Another simple approach to concatenating two or more strings in Powershell is through the use of join operator. The command sequence for using the join switch in Powershell is shared below. For this example, we will follow the process below:

  • We will define three strings in three different variables $str1, $str2 and $str3.
  • We will use the join switch to concatenate all the three strings.
  • We will store the concatenated string in a variable.
  • We will use the Write-output cmdlet in Powershell to display results on the screen. Alternatively, we can also export the output of the command to a static file.

The exact command sequence and the results of the commands are shared below in a screenshot. We are using space as a separator between two strings in the command below. You can see that we have added an empty space ” ” after the -join operator in the Powershell command directive below.

Concatenate strings using join operator in Powershell

Concatenate strings using concat() function in Powershell

We have a pre-existing function in Powershell that can be used to concatenate two or more strings. The concat() function can be used to combine two or more strings for a composite string output. For using the concat() function, we will follow the process below:

  • Define three strings in three variables $str1, $str2 and $str3.
  • Use the concat( ) function to combine these strings and create a System String object. We will store this System string object as a variable.
  • Use the write-output cmdlet in Powershell to display the results or concatenated string in Powershell.

The command sequence and the command output is displayed below as a screenshot.

Concatenate strings in Powershell using join string

We can use the “join string” directive in Powershell to concatenate two or more strings. The exact command sequence is shared below. For joining two or more strings in Powershell using ‘join string’ directive, we will use the following approach:

  • Create three strings and assign them to variables $str1, $str2 and $str3.
  • Use “join string” to concatenate these strings into a new variable called $concat4.
  • Use the write-output cmdlet to display the concatenated strings on the screen.

The sequence of commands and the exact command output are shared below for your ready reference.

Concatenate two or more strings using join string in Powershell.

Concatenate strings using -f or format switch in Powershell

We can use the format switch, -f, to concatenate two or more strings. Before we can use the format switch, we will need to place each string data type into index positions or array positions starting from the array pointer {0}. So, the first string goes into array position {0}. The second string will go into {1} position in the array and the third string will go into {2} position into the array.

Once we have placed the strings in array positions, we can concatenate these strings using the -f format switch.

The exact command sequence is shared below in the screenshot.

Using -f format to concat two or more strings in Powershell.
You can see that we have created an array comprising of “{0} {1} {2}” strings in the command. Values to these array positions have been assigned using the string variables that we had declared earlier. The merged strings are displayed on the screen using the write-output cmdlet in Powershell.

Summary

In this Powershell tutorial, we have seen five different methods by which we can concatenate strings in Powershell. The methods we have discussed include:

  • Concatenate strings using ‘+’ operator
  • Concatenate strings using the join operator
  • Concatenate strings using the concat() function in Powershell
  • Concatenate strings using the join-string command qualifier
  • Concatenate strings using -f format switch in Powershell

We can display the output on the screen using the write-output Powershell cmdlet. Or, we can redirect the write-output cmdlet output to a static text file on the computer.

You may like to read more Powershell tutorials below:

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Rajesh Dhawan

Rajesh Dhawan is a technology professional who loves to blog about smart wearables, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.