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.

PowerShell Concatenate String with Examples

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. To concatenate strings, the usual + operator is used:

$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 multiple strings:

$A = “First”
$B = “Second”
$C = “Third”
[string]::Concat($A,$B,$C)

powershell string concatenation

The + operator is not the only one we must use to concatenate strings — we can also use separators like commas (,). Remember to enclose your string variables with double quotes “”, or the variable will interpret your separated string values as a list property.

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

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

powershell concatenate strings

In some cases, you need to insert a substring into the source string in your scripts 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 concatenate string and variable

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

concatenate string powershell

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 concat string

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,".")

PowerShell: How to Concatenate String with Variable?

You can concatenate multiple strings and variables into a single string. For example, like this:

$w = "World"

"Hello " + $w + "!"

The result will be:

Hello world!

But you can simplify this construction by simply placing the variable inside the string:

"Hello $w!"

powershell combine strings

As you can see, the syntax and readability of the variable-string PowerShell command has been greatly simplified.

Let’s try to use a more complex example of concatenating a PowerShell string with a variable. For example, you want to use a method or property of a variable right on the line.

$date=Get-Date

"Current year is $date.year !"

powershell append string

When running such a command, PowerShell was unable to interpret the method we wanted to apply to the variable. The PS interpreter simply treats the dot as a line continuation.

To bypass this problem, you need to take the variable in brackets:

"Current year is $($date.year)!"

powershell concatenate string variables

A similar method can be used when you need to insert a variable in the middle of a string:

$insert="Shell"

Write-Host "Power$insertISE"

The PowerShell interpreter simply cannot identify where the variable name ends and where the text begins. To concatenate such a string, one of the following syntaxes must be used:

Write-Host "Power${insert}ISE"

or

Write-Host "Power$($insert)ISE"

concat string powershell

If you need to add a specific string to a variable, you can use the obvious syntax:

$var1= "Hello"

$var1= $var1 + "World!"

Write-Host $var1

Or use a simplified syntax that is familiar to almost any programming language:

$var1= "Hello"

$var1 += "World!"

Write-Host $var1

powershell add to string

When concatenating strings, you can use the comma (,) separator instead of the + operator. In this case, make sure to enclose your string variables in double quotes “”, otherwise the variable will interpret your separated string values as a list property. For example:

$concatString = "$String1 , $String2 , $String3"

Write-Output $concatString

When concatenating an Integer variable with a string variable, an error may occur:

[Int]$integer1 = 0

$String1 ="info"

$concatString = $integer1 + $String1

Cannot convert value “info” to type “System.Int32”. Error: “Input string was not in a correct format.”

powershell string concat

This error occurs because the variable of type Int is called first in the expression. If you call it after a string variable, PowerShell will automatically convert the integer value to a string:

$concatString = $String1 + $integer1
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.

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