How to Join or Concatenate String and Number Using PowerShell

Combining string and number is a frequent task in PowerShell, involving merging text and numerical values. This can be accomplished through various methods employing different operators and delimiters such as space, semicolon, comma, etc. This article will explore various methods for concatenating strings and numbers in PowerShell.

Let’s explore a few methods for string concatenation in PowerShell:

#1 – Add String and Number Using the + (Plus) Operator

The + (plus) operator stands out as the frequently used operator in PowerShell for string concatenation, allowing the combination of a string with another string. Additionally, this operator can append a numerical value to a string. Here’s an example for clarification:

$string = "Windows"
$number = 10
$result1 = $string  +  $number
Write-Host $result1

$result2 = $string  + ' ' + $number
Write-Host $result2

In this example, first, we combine text with a number, and then we join the string value with the number using a space delimiter.

#Output 1:
Windows10
#Output 2 (join with space):
Windows 10

If you attempt to concatenate a number with a string using the + (plus) operator in PowerShell instead of combining a string with a number, you will encounter the error “Input string was not in a correct format.” This is because PowerShell performs concatenation from left to right, attempting to convert the string into an integer and encountering a failure in the process.

#Example:
$number = 10
$string = "Windows"
$result = $number + $string
Write-Host $result

#You will receive the following error message:
Cannot convert value "Windows" to type "System.Int32". Error: "Input string was not in a correct format."

To overcome this issue, we can resolve it by initially adding an empty string and then combining the values using the + operator.

$string1 = ""
$number = 10
$string2 = "Windows"
$result = $string1 + $number + $string2
Write-Host $result

#Output:
10Windows

#2 – Concatenate String and Number Using the -Join Operator

With the -Join operator, we can combine a string with a number using the following code:

$string = "Windows"
$number = 10
$result = -Join($string, $number)
Write-Host $result

#Output:
Windows10

We can use the -Join operator to merge a string and a number with different delimiters such as space, comma, semicolon, etc. In the example below, we will demonstrate the concatenation of string and number values with a space delimiter.

$string = "Windows"
$number = 10
$delimiter = " "
$result = ($string, $number) -join $delimiter							
Write-Host $result

#Output:
Windows 10

#3 – Concatenate String and Number Using the Join() Method (.NET)

We can use the .NET based Join() method to concatenate a string with a number. Here is an example of combining a string and a number with the ‘_’ (underscore) delimiter.

$string = "Windows"
$number = 10
$delimiter = "_"
$result = [System.String]::Join($delimiter, $string, $number)
Write-Host $result

#Output:
Windows_10

#4 – Combine String and Number Using the Concat() Method (.NET)

The following example uses the .NET Concat() method to merge a string and a number.

$string = "Welcome"
$number = 2024
$result = [System.String]::Concat($string," ",$number)
Write-Host $result

#Output:
Welcome 2024

#5 – Combine String and Number Using the -f (Format) Operator

The PowerShell -f Format operator is useful for formatting strings as per the desired pattern. Through the -f operator, we can effectively merge a string and a number in a specified format. The following example concatenates a string and a number using the -f operator.

$name = "Kevin"
$age = 30
$message = "My name is {0} and I am {1} years old." -f $name, $age
Write-host $message

#Output:
My name is Kevin and I am 30 years old.

#6 – Join String and Number using the String-Builder Method

We can use the String-Builder method to concatenate large string with numbers. Here is an example:

#Import String-Builder from .NET
$stringbuilder = New-Object System.Text.StringBuilder

#Append the string and number into the StringBuilder
[void]$stringbuilder.Append("Welcome")
[void]$stringbuilder.Append(" to ")
[void]$stringbuilder.Append(2024)

#Convert the StringBuilder to string 
$result = $stringbuilder.ToString()
Write-Host $result

#Output:
Welcome to 2024

Select the method that is most suitable for your specific use case. PowerShell provides versatility in concatenating strings and numbers, enabling you to tailor your approach to different scenarios and preferences.

Advertisement

Leave a Comment