Powershell

Powershell Concatenate String

PowerShell Scripting is one of the most valuable skills any system administrator can possess. It allows for the creation of simple to complex tools that can automate tasks in a matter of seconds.

In this tutorial, we will learn how we can work with strings by learning how to concatenate strings in PowerShell.

Let’s get started.

PowerShell Concatenate Strings – The Addition Operator

The most basic and standard method of concatenating strings in PowerShell is the addition operator. This is very useful when you need to connect two or more strings.

Hence, ensuring that all the passed variables hold string types before concatenating is good practice.

Let us look at an example of using the addition operator to join two strings.

$url = "linuxhint.com"
$proto = "https://"

$site = $proto + $url
Write-Output $site

 
In the example above, we define two string variables that hold the URL and the protocol for a specific web address.

We then use the addition operator to concatenate the two strings and store the result in a new variable.

Once we run the code above, we should get an output as shown:

. " concate_strings.ps1"
https://linuxhint.com

 

PowerShell Concatenate Strings – The -f Operator

The second method to concatenate strings is the -f operator. This operator allows you to specify the string variables as arguments and return one value.

Take the example presented below:

$url = "linuxhint.com"
$proto = "https://"

$site = "{0} {1}" -f $proto,$url
Write-Output $site

 
In this example, we use the -f operator to pass the strings we wish to concatenate as parameters. PowerShell will then take the strings in the order they are presented and join them into one.

This method is very similar to the f-string formatting in Python.

The resulting value is as shown:

.\concate_strings.ps1"
https:// linuxhint.com

 

PowerShell Concatenate Strings – Using a Separator.

PowerShell also supports delimiters as a method of concatenating strings. In this case, we pass the string variables we wish to concatenate, separated by the target delimiters.

For example:

$firstName = "Pauline"
$lastName = "Yue"

$fullName = $firstName, $lastName
Write-Output $fullName

 
In this example, we pass the delimiter value as a comma. This allows us to pass multiple strings and get one value, as shown in the output:

Pauline
Yue

 

PowerShell Concatenate Strings – Using the String Builder Class

There is another, less conspicuous method of concatenating strings in PowerShell. C# has a class called StringBuilder we can use to do this.

The StringBuilder class in C# allows you to create mutable string characters. However, we can use it to concatenate strings in PowerShell, as shown in the example below.

$url = "linuxhint.com"
$proto = "https://"
$site = New-Object -TypeName System.Text.StringBuilder
$t = $site.Append($proto)
$t = $site.Append($url)
$site.ToString()

 
In the code above, we start by defining two variables that hold the strings we wish to concatenate.

In the third line, we create a new variable that holds the full string object. Note that we define it as a StringBuilder type.

Finally, we use the newly created object and the Append method to add the strings together.

You may ask, “Why would I ever need to jump to c# to join strings in PowerShell?” Although we can’t break down the full benefit in this article, using StringBuilder removes certain limitations in PowerShell, such as mutability.

PowerShell Concatenate String – Using Concat Function()

Lastly, PowerShell’s concat() function allows joining strings in a cleaner method.

Take an example code as shown below:

$url = "linuxhint.com"
$proto = "https://"
$site = [System.String]::Concat($proto,$url)
Write-Output $site

 
In the example above, we use the System.String::Concat() function to concatenate the strings.

This is the cleanest method of joining strings. In addition, it is a native PowerShell, and you do not have to create new Objects and convert them back to a string to do this.

This should return output as:

“.\concate_strings.ps1"
https://linuxhint.com

 

Closing

In this article, you learned various methods and techniques of concatenating strings in PowerShell.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list