In today’s world, data is the most powerful asset for every organization. But this data is not always structured the way we want, especially for analysis. Hence, it becomes necessary to concatenate or join two or more fields to make it ideal for analysis. PowerShell offers a lot of versatility in concatenation, so in this article, we will explore all that PowerShell offers in this area and how you can take advantage of it.
PowerShell concatenation with strings
String concatenation is something that we use commonly to create the right data. Here is how to concatenate them in PowerShell.
Concatenate two strings
Joining together two strings is fairly simple in PowerShell. All that you have to do is add a “+” symbol between your strings.
For example, let’s say you want to concatenate John and Galt with space in between. To do that:
$name = 'John' + " " + 'Galt' $name
The output is John Galt.
Concatenate two string variables
In the real world, the chances of concatenating two strings or words are less. Rather, you will fetch data from the database, store it in a variable, and process this variable.
Here is how you can concatenate two string variables.
$firstname = 'John' $lastname = 'Galt' $firstname + " " + $lastname
The output is John Galt.
Concatenate integers with strings
Sometimes, you may have to concatenate two different data types, such as a string and an integer. The good news is PowerShell makes it so easy to combine different data types provided you understand its processing.
Let’s understand this with an example:
$number1 = 10 $string 1 = 'Snow'
Now, if you concatenate the two like this:
$number1 + " " + $string1
You’ll get an error — Cannot convert value “Snow” to type “System.Int32”. Error: “Input string was not in the correct format.”
You get this error because PowerShell concatenation happens from left to right, so it tries to convert the string into an integer and fails.
But you can do it the other way and it will work.
$string1 + " " + $number1
The output is Snow 10
As you may have guessed, PowerShell concatenation has converted the integer into a string, so there is no error this time.
Single quotes and double quotes
When to use a single quote and a double quote? What is the difference in output?
This is a question that all of us have had at some point. So, let’s talk a bit about these quotes and their differences.
First off, know that both the quotes serve the same purpose, which is to delimit string value. In general, you must use only single quotes to denote a string.
$variable = 'Hello world' Write-Host $variable
Likewise, you can output the string directly like this:
Write-Host 'Hello world'
The output is Hello World in both cases.
So, when do you use double quotes? There are three situations where you’d want to use double-quotes.
PowerShell concatenation of string and variable
A common use of double quotes is when you want to concatenate a string and a variable.
For example:
$variable = 'World' Write-Host "Hello $variable"
The output will be Hello World.
Here, if you use single quotes, PowerShell will not process the value of variables but will display it as a single string.
For example, if you replace the double quotes with single quotes like this:
$variable = 'World' Write-Host 'Hello $variable'
The output will be Hello $variable
Delimiting a string within a string
Before you wonder what in the world it means, let’s get to an example right away!
$query = "SELECT * FROM products WHERE Category LIKE '%Sauce'"
As you can see, the double quotes are used to delimit the string (Sauce) that is enclosed in single quotes. You’ll likely use double quotes when you build a database query like this.
Using the ESC character
When you want to use the “`” or the escape character, use the double quotes as they are not parsed within single quotes.
Besides these three situations, use single quotes as it is considered good programming practice.
Built-in PowerShell concatenation commands
Earlier, we talked about PowerShell concatenation with just the “+” sign. You can also concatenate with some built-in PowerShell commands. Let’s take a look at these commands.
Join
You can use the -join operator to concatenate two strings.
For example:
$string1 ='Bright' $string2 ='Sunny' $string1, $string2 -join "/"
The output is Bright/Sunny
As you can see what this -join operator does is it not only concatenates strings but also inserts the delimiters or words you want. But the delimiter will be applied between every two words.
$string3 = 'Happy' $string1, $string2, $string3 -join "/"
The output is Bright/Sunny/Happy
This PowerShell concatenation command can be particularly helpful when you want to have a delimiter or space between every word. Instead of delimiter, you can also have words, numbers, symbols, or just about anything else that you want between each word.
For example:
$string1, $string2, $string3 -join " and "
The output is Bright and Sunny and Happy
Format operator
The format operator, denoted by -f, is used to format strings the way you want. And this formatting includes PowerShell concatenation as well.
It is most useful while working with arrays.
Let’s see a few ways to use them:
[array]$daysoftheweek = ('Monday', 'Tuesday', 'Wednesday') "Today is {1}" -f $daysoftheweek
The output is Today is Tuesday.
Now, you can change the array places to get a different day.
"The first two days of the week are {0} and {1}" -f $daysoftheweek
The output is The first two days of the week are Monday and Tuesday
You can also do a lot of formatting with this formatting operator besides just concatenating.
Concat()
Concat() is another built-in function that supports PowerShell concatenation.
To use this:
$string1 = "Welcome" $string2 ="to" $string3 = "Atlanta" $output = [System.String]::Concat($string1," ", $string2," ", $string3) Write-Host $output
The output is Welcome to Atlanta.
As you can see, this is a simple function that works only with strings. Even if you enter integers, the values are automatically converted to strings.
PowerShell concatenation with a string builder object
The last function that we’ll see is the string builder. This is an object type and you can add values to it.
Let’s see an example to better understand:
$greeting = New-Object -TypeName System.Text.StringBuilder $name = 'John' $null = $greeting.Append("Hello ") $null = $greeting.Append($name + ". ") $null = $greeting.Append("Welcome to Atlanta") Write-Host $greeting.ToString()
The output is Hello John. Welcome to Atlanta.
Besides these built-in functions, there are a host of other string functions that can come in handy for string manipulation in PowerShell.
Have you tried these PowerShell concatenation options? Do you know other ways to do it? Please share them in the comments section.
Featured image: Pixabay