How to Concatenate String and Integer in PowerShell?

Recently, I was working on a PowerShell task that required me to concatenate a string with an integer. I tried different methods. Let me show you how to concatenate strings and integers in PowerShell.

To concatenate a string and an integer in PowerShell, you can use the + operator, which automatically converts the integer to a string: $result = $string + $integer. For more complex formatting, the -f operator allows you to insert variables within a string template: $result = “The count is {0}” -f $integer. String interpolation, using double quotes, is another readable method: $result = “$string $integer”.

Concatenate String and Integer in PowerShell

Now, let me explain various methods to concatenate a string and an integer in PowerShell. You can also check an article on Concatenate Strings in PowerShell.

Method 1: Using the + Operator

The simplest way to concatenate strings and integers in PowerShell is by using the + operator. Here is an example that is easy to understand.

# Define a string and an integer
$string = "The total count is "
$integer = 42

# Concatenate using the + operator
$result = $string + $integer

# Output the result
Write-Output $result

Output:

The total count is 42

In this example, the + operator is used to concatenate the string $string with the integer $integer. PowerShell automatically converts the integer to a string before concatenation.

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

Concatenate String and Integer in PowerShell

Method 2: Using the -f Operator (Format Operator)

The -f operator is another powerful method for concatenating strings and integers in PowerShell. It allows for more complex formatting and is useful when multiple variables need to be included in a string.

Here is a complete example and the full script.

# Define a string and an integer
$string = "The total count is {0}"
$integer = 42

# Concatenate using the -f operator
$result = $string -f $integer

# Output the result
Write-Output $result

Output:

The total count is 42

In this example, the {0} placeholder in the string $string is replaced by the value of $integer using the -f operator.

Like the above script, I executed this script also, and you can see exactly the same result; check the screenshot below:

concatenate strings and integers in PowerShell

Read Convert Multiline String to Single Line in PowerShell

Method 3: Using Subexpression $()

This method is a little tricky.

Subexpressions allow you to insert the result of an expression into a string in PowerShell. This method is useful when performing calculations or calling functions within a string.

Here is the complete PowerShell script.

# Define a string and an integer
$string = "The total count is "
$integer = 42

# Concatenate using subexpression $()
$result = "$string$($integer)"

# Output the result
Write-Output $result

Output:

The total count is 42

In this example, the subexpression $($integer) is used to insert the value of $integer into the string.

I executed the above script, and you can see the output in the screenshot below:

powershell concatenate string and int

Method 4: Using String Interpolation

String interpolation is a feature in PowerShell that allows you to embed variables directly within a string. This method is very readable and easy to use.

Here is the complete PowerShell script.

# Define a string and an integer
$string = "The total count is"
$integer = 42

# Concatenate using string interpolation
$result = "$string $integer"

# Output the result
Write-Output $result

Output:

The total count is 42

In this example, the variables $string and $integer are embedded directly within the string using double quotes.

Read Add Double Quotes in a String in PowerShell

Method 5: Using StringBuilder

Let me show you another method for concatenating a string and an integer. It will be helpful if you have some knowledge of .Net.

The StringBuilder class is part of the .NET framework and is useful for efficiently concatenating a large number of strings. It also provides methods for appending strings.

Here is a complete script.

# Load the StringBuilder class
Add-Type -AssemblyName 'System.Text'

# Create a StringBuilder object
$stringBuilder = New-Object System.Text.StringBuilder

# Define a string and an integer
$string = "The total count is "
$integer = 42

# Append the string and integer
$stringBuilder.Append($string) | Out-Null
$stringBuilder.Append($integer) | Out-Null

# Convert the StringBuilder object to a string
$result = $stringBuilder.ToString()

# Output the result
Write-Output $result

Output:

The total count is 42

In this example, the StringBuilder class is used to append the string $string and the integer $integer, and the result is converted to a string using the ToString() method.

Read Concatenate String and Variable in PowerShell

Method 6: Using Join-String (PowerShell 7+)

PowerShell 7 introduced the Join-String cmdlet, and this cmdlet implied the concatenation process. This method is useful for concatenating multiple strings with a delimiter.

Here is an example.

# Define a string and an integer
$string = "The total count is"
$integer = 42

# Concatenate using Join-String
$result = "$string $integer" | Join-String

# Output the result
Write-Output $result

Output:

The total count is 42

In this example, the Join-String cmdlet is used to concatenate the string $string and the integer $integer.

You can see the output in the screenshot below, I executed the above script.

powershell concatenate string and int using Join-String

Read Split a String into Variables in PowerShell

Method 7: Using Format-String (PowerShell 7+)

Let me now explain the last method, which is Format-String.

The Format-String cmdlet, introduced in PowerShell 7, provides another way to format and concatenate strings.

Here is the complete script.

# Define a string and an integer
$string = "The total count is {0}"
$integer = 42

# Concatenate using Format-String
$result = Format-String -Format $string -Args $integer

# Output the result
Write-Output $result

Output:

The total count is 42

In this example, the Format-String cmdlet is used to replace the {0} placeholder in the string $string with the value of $integer.

In this tutorial, I have explained several methods for concatenating strings and integers in PowerShell. We saw examples for each method, and I hope this helps you.

  1. Using the + Operator: Simple and easy method.
  2. Using the -f Operator: Powerful for complex formatting.
  3. Using Subexpression $(): Useful for embedding expressions in strings.
  4. Using String Interpolation: Readable and easy to use.
  5. Using StringBuilder: Efficient for large-scale concatenation.
  6. Using Join-String: Simplifies concatenation with delimiters (PowerShell 7+).
  7. Using Format-String: Provides another way to format and concatenate (PowerShell 7+).

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.