PowerShell: Concatenate String

One of the main string operations in PowerShell is concatenation. It is the operation of joining multiple strings into one. Most often, the string concatenation operation is performed using the “+” operator. Additionally, PowerShell has several other ways to concatenate strings.

This article will help you to find few more popular string concatenation operators you can use in your scripts.

Recall that the output of any PowerShell command is always not text, but an object. If you’ve assigned a string value to your PowerShell variable, that string is a separate object with its own properties and methods you can use to process text.

Let’s have a look at the simplest example of concatenating two variables in PowerShell:

$name = “John”

$message = “, please, check the mailbox”

Now  concatenate these two lines and show the output:

$text = $name + $message

Write-Host $text

 The resulting variable now contains the concatenation value of the two string variables.

As it is, you can join many strings at once:

$text = $name + $message + “Some Text” + $info + $ticketnumber + “!”

You can also use the Concat method to concatenate strings:

$A = “First”

$B = “Second”

$C = “Third”


If you want to concatenate multiple strings, use the Join method.

 In this example, we use space and a colon between the joined strings.

[string]::Join(‘: ‘,$A,$B,$C)

In some cases, in your scripts, you need to insert a substring into the source string starting at the specified character. Use the Insert method for this.

 In this example, we add the value of the variable $A to the string value of the variable $text after 5 characters:

$text2=$text.Insert(5,(“ :”+$a))

As you can see, inside the Insert method, we performed another concatenation to add specific characters before the added text.

Because the expanding string can become difficult to read, you can use the substitution values and the format specifier. In this case, you can use the special operator –f to concatenate strings. First, specify the format in which you want to receive the resulting string, and then pass the variable names:




{0}.{1}@{2} -f $firstname,$lastname,$domain

To remove characters starting with the specified one from a string, use Remove:


To replace certain text in a string with another:


 By using the Length property, you can find out the length of the string (how many characters it contains):


Hint. To add a line to the left or right of the text, use the PadLeft and PadRight methods:


Leave a Reply

Your email address will not be published. Required fields are marked *

Enter Captcha Here :