PowerShell: Concatenate String

One of the main string operations in PowerShell is concatenation. String Concatenation 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. In this article, we’ll take a look at a 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 look at the simplest example of concatenating two variables in PowerShell:

$name = “John”

$message = “, please, check the mailbox”

Now let’s concatenate these two lines and show the output:

$text = $name + $message

Write-Host $text

powershell concatenate string

As you can see, the resulting variable now contains the concatenation of the value of the two string variables.

Likewise, 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”

powershell concat string

If you want to concatenate multiple strings, but using a delimiter, use the Join method. In this example, we want a space and a colon between the joined strings.

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

powershell concatenate

In some cases, in your scripts, you need to insert a substring into the source string starting at the specified character. You can 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))

powershell concat

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

READ ALSO  PowerShell: Switch Statement Usage

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, you specify the format in which you want to receive the resulting string, and then you pass the variable names:




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

powershell combine strings

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


Or you can replace certain text in a string with another:


powershell concatenate string and variable

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:

Cyril Kardashevsky

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.