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 $addressIn this example, we are concatenating the city and state names using the + operator, resulting in the output:
New York, New YorkAlso, you can see the exact output in the screenshot below after I executed the above script using VS code.

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 $addressHere, the $() syntax allows the variables $city and $state to be evaluated within the string, producing:
Los Angeles, CaliforniaNow, if you look at the screenshot below, you can see the exact required output:

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 $addressThe -f operator formats the string by replacing {0} and {1} with $city and $state, respectively, resulting in:
Chicago, IllinoisRead 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 $addressThe -join operator joins the elements of the $parts array with “, ” as the separator, giving us:
Houston, TexasI executed the above script on my local system, and you can see the exact output in the screenshot below:

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 $addressIn this example, StringBuilder is used to concatenate the city and state names efficiently, resulting in:
Phoenix, Arizona, Dallas, TexasConcatenate 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 $addressOutput:
SanFranciscoCaliforniaIf you look at the screenshot below, you can see the exact output. I executed the above PowerShell script using VS code.

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 $addressOutput:
AustinTexas3. 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 $addressOutput:
MiamiFlorida4. 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 $addressOutput:
SeattleWashington5. 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 $addressOutput:
DenverColoradoI executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

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:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.