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”
[string]::Concat($A,$B,$C)

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.

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:

$firstname=”John”

$LastName=”Brion”

$domain=”theitbros.com”

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

powershell combine strings

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

$text2.Remove(15)

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

$text2.Replace(“John”,”Cyril”)

powershell concatenate string and variable

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

$text2.Length

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

$text2.PadLeft(3,".")
I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.
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.