How to Concatenate String and Variable in PowerShell?

Recently, I was required to concatenate strings with variables in PowerShell. I tried different methods. In this tutorial, I will explain how to concatenate strings and variables in PowerShell with examples.

To concatenate strings with variables in PowerShell, you can use the + operator, which directly combines strings and variables. For example, $city = “New York”; $state = “New York”; $address = $city + “, ” + $state results in “New York, New York”. You can also use subexpressions $() within double-quoted strings to evaluate variables, such as $address = “$($city), $($state)”. The -f format operator can also be used to insert variables into a string, like $address = “{0}, {1}” -f $city, $state. For joining multiple strings in an array, the -join operator is ideal, as in $address = $parts -join “, “.

Concatenate String and Variable in PowerShell

Now, let us understand each method with examples.

Method 1: Using the + Operator

To concatenate strings with variables in PowerShell, you can use the + operator. This is the simplest and most used method. Here is an example with the complete script.

$city = "New York"
$state = "New York"
$address = $city + ", " + $state
Write-Host $address

In this example, we are concatenating the city and state names using the + operator, resulting in the output:

New York, New York

Also, you can see the exact output in the screenshot below after I executed the above script using VS code.

Concatenate String and Variable in PowerShell

Read Concatenate String and Integer in PowerShell

Method 2: Using Subexpression $()

Here is another method.

In PowerShel, when you need to include variables within a string, you can use the subexpression $() to evaluate the variables within a double-quoted string.

Here is the complete example and the code.

$city = "Los Angeles"
$state = "California"
$address = "$($city), $($state)"
Write-Host $address

Here, the $() syntax allows the variables $city and $state to be evaluated within the string, producing:

Los Angeles, California

Now, if you look at the screenshot below, you can see the exact required output:

Concatenate a String and a Variable in PowerShell

Method 3: Using the -f Format Operator

The -f The format operator is another method for formatting strings in PowerShell. It allows you to insert variables into a string. Below is an example.

Example:

$city = "Chicago"
$state = "Illinois"
$address = "{0}, {1}" -f $city, $state
Write-Host $address

The -f operator formats the string by replacing {0} and {1} with $city and $state, respectively, resulting in:

Chicago, Illinois

Read Concatenate Strings Inside Loops in PowerShell

Method 4: Using the -join Operator

The -join operator in PowerShell is useful when you want to concatenate multiple strings stored in an array. This method is convenient when dealing with a list of strings.

You can understand from the below example

$parts = @("Houston", "Texas")
$address = $parts -join ", "
Write-Host $address

The -join operator joins the elements of the $parts array with “, ” as the separator, giving us:

Houston, Texas

I executed the above script on my local system, and you can see the exact output in the screenshot below:

Concatenate String and Variable in PowerShell using join operator

Method 5: Using StringBuilder for Performance

If you want to concatenate a large number of strings or you want to concatenate strings inside a loop, you can use the StringBuilder method.

Here is an example:

Add-Type -AssemblyName "System.Text"
$stringBuilder = New-Object System.Text.StringBuilder
$cities = @("Phoenix", "Arizona", "Dallas", "Texas")

foreach ($part in $cities) {
    [void]$stringBuilder.Append($part + ", ")
}

# Remove the trailing comma and space
$address = $stringBuilder.ToString().TrimEnd(", ")
Write-Host $address

In this example, StringBuilder is used to concatenate the city and state names efficiently, resulting in:

Phoenix, Arizona, Dallas, Texas

Concatenate string and variable without space in PowerShell

If you have a specific requirement to concatenate a string and a variable without space in PowerShell, you can use the methods below.

1. Using the + Operator

The + operator allows you to directly concatenate strings and variables without adding spaces in PowerShell.

Let me show you an example for you to understand it better.

$city = "SanFrancisco"
$state = "California"
$address = $city + $state
Write-Host $address

Output:

SanFranciscoCalifornia

If you look at the screenshot below, you can see the exact output. I executed the above PowerShell script using VS code.

Concatenate string and variable without space in PowerShell

2. Using Subexpression $()

You can also use the subexpressions within double-quoted strings to concatenate variables without spaces in PowerShell.

Here is an example.

$city = "Austin"
$state = "Texas"
$address = "$($city)$($state)"
Write-Host $address

Output:

AustinTexas

3. Using -f Format Operator

The -f format operator can also be used to concatenate strings without spaces in PowerShell by placing the variables directly next to each other within the format string.

Here is an example.

$city = "Miami"
$state = "Florida"
$address = "{0}{1}" -f $city, $state
Write-Host $address

Output:

MiamiFlorida

4. Using StringBuilder

In PowerShell, you can also use StringBuilder to concatenate strings without spaces efficiently.

Here is an example to help you understand it better.

Add-Type -AssemblyName "System.Text"
$stringBuilder = New-Object System.Text.StringBuilder
$cities = @("Seattle", "Washington")

foreach ($part in $cities) {
    [void]$stringBuilder.Append($part)
}

$address = $stringBuilder.ToString()
Write-Host $address

Output:

SeattleWashington

5. Using Curly Braces

This is the final method: use Curly braces to concatenate a string and variable without any space in PowerShell.

Using curly braces {} around variables within double-quoted strings can ensure no unintended spaces.

Here is an example to help you understand it better.

$city = "Denver"
$state = "Colorado"
$address = "${city}${state}"
Write-Host $address

Output:

DenverColorado

I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

PowerShell Concatenate string and variable without space

Conclusion

In PowerShell, you can concatenate a string and variable without any space using various methods like using the + operator, subexpressions, the -f format operator, StringBuilder, or curly braces, etc.

I hope you know how to concatenate strings and variables in PowerShell using various methods.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.