Home » PowerShell » PowerShell Concatenate String with Examples

PowerShell Concatenate String with Examples

In PowerShell Concatenate String is achieved by using the + operator. There are different ways to concatenate strings using the -f operator, and join operator. PowerShell built-in Concat() function is used for string concatenation.

In this article, we will discuss different ways for PowerShell string concatenation, append the string, concatenate two variables and concatenate the string with integers.

Let’s understand PowerShell string concatenation using different ways with examples.

Using + operator for PowerShell string concatenate

+ operator is used in PowerShell to concatenate string.

$firstName = "Shell"
$lastName = "Geek"

$fullName = $firstName + $lastName

Write-Host "Concatenate String:"
$fullName

In the above concatenation of two variables example, the $fullName variable contains a combined string.

PowerShell string concatenation using + operator
PowerShell string concatenation using + operator

Cool Tip: How to add new line to string in PowerShell!

Using -f operator to PowerShell concatenation of strings

Using the -f operator in PowerShell, you can perform PowerShell concatenation of strings.

$Heading = "PowerShell Concatenate of String"
$Author = "By ShellAdmin"

$Title = "{0} {1}" -f $Heading,$Author

Write-Output $Title

In the above PowerShell concatenate string example,

we have two string variables $Heading and $Author.

Using the -f operator, it concatenates two strings variables.

PowerShell Concatenate two variables using -f operator
PowerShell Concatenate two variables using the -f operator

Cool Tip: How to add a new line to string in PowerShell!

Using the join operator to join strings in PowerShell

Using the join operator in PowerShell to join strings using the separator.

$Title = $Heading,$Author -join "-" 

Write-Output $Title

In the above string concatenate example,

join operator in PowerShell use – separator to join two string variables.

PowerShell join String using join operator
PowerShell join String using the join operator

Cool Tip: How to create a multiline string in PowerShell!

PowerShell String Concatenation with Integers

To perform concatenation of string with integers, the string variable has to be used first in the expression and later integer variable.

For example:

$strValue = "Total value of this asset:"
$intValue = 15000

$strValue + $intValue

In the above string concatenate with integer example,

$strValue variable contains string data type value.

$intValue variable contains integer data type value.

The output of the above command is given

PowerShell String Concatenation with Integer
PowerShell String Concatenation with Integer

Cool Tip: PowerShell echo equivalent command in Windows!

However, if we swap the position of variables like

$intValue + $strValue

It will throw an exception, as it will try to convert the string value to int32 type and results in the input string, not in a correct format error.

PowerShell join integer with string error
PowerShell join integer with string error

Cool Tip: Replace text in a string using PowerShell!

Using StringBuilder to PowerShell Concatenate String

Using System.Text.StringBuilder type in PowerShell, you can append string using Append().

$Heading = "PowerShell Concatenate of String"
$Author = "By ShellAdmin"
$WebTitle = New-Object -TypeName System.Text.StringBuilder
$temp = $WebTitle.Append($Heading)
$temp = $WebTitle.Append($Author)                 
$WebTitle.ToString()  

In the above example of PowerShell append string,

Using System.Text.StringBuilder type, it creates a new object as $WebTitle

We have used Append() function to append the string to the object.

To print object value, we need to convert it to a string using the ToString() function.

PowerShell Append to String
PowerShell Append to String

PowerShell Tip: Use PowerShell $null to check if a variable is empty!

Using PowerShell Concat() function for string concatenation.

Using PowerShell concat() function, it concate differnt strings.

$Heading = "PowerShell Concatenate of String"
$Author = "By ShellAdmin"
$WebTitle = [System.String]::Concat($Heading,$Author)
Write-Output $WebTitle

In the above example of PowerShell Concat string, it uses Concat() function to combine two variables.

PowerShell Concat two variables
PowerShell Concat two variables

Cool Tip: String interpolation using PowerShell!

Conclusion

In this article, we have covered the PowerShell concatenation of string.

In PowerShell, there are multiple ways to perform string concatenation.

Using + operator, -f operator, or join operator, string concatenation can be achieved.

Cool Tip: How to use PowerShell Exit keyword!

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.