Table of Contents
Using Operators
To concatenate string in PowerShell:
- Declare and initialize variables which contain string-type values.
- Use
+
,-f
, and-join
operators to concatenate values of all variables we created in the first step.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$greetings = "Hi" $first_name = "Mehvish" $last_name = "Ashiq" $plus_without_separator = $greetings + $first_name + $last_name $plus_with_separator = $greetings +" "+ $first_name +" "+ $last_name $f_without_separator = "{0}{1}{2}" -f $greetings, $first_name, $last_name $f_with_separator = "{0} {1} {2}" -f $greetings, $first_name, $last_name $join_without_separator = $greetings, $first_name, $last_name -join "" $join_with_separator = $greetings, $first_name, $last_name -join " " $props = $([ordered]@{ plus_without_separator=$plus_without_separator plus_with_separator=$plus_with_separator f_without_separator=$f_without_separator f_with_separator=$f_with_separator join_without_separator=$join_without_separator join_with_separator=$join_with_separator }) $results=@() $results += New-Object psobject -Property $props Write-Output $results |
1 2 3 4 5 6 7 8 |
plus_without_separator : HiMehvishAshiq plus_with_separator : Hi Mehvish Ashiq f_without_separator : HiMehvishAshiq f_with_separator : Hi Mehvish Ashiq join_without_separator : HiMehvishAshiq join_with_separator : Hi Mehvish Ashiq |
We can use the +
, -f
, and -join
operators to concatenate strings with/without separators in PowerShell. Let’s understand the use and difference of these operators below.
First, we declared and initialized three variables, $greetings
, $first_name
, and $last_name
containing Hi
, Mehvish
, and Ashiq
values, respectively.
Note that PowerShell variables are prefixed with a
$
sign, for example,$VariableName
.
Next, we used the +
operator to concatenate values of all variables with/without a separator. We used a single space to separate one string from the other; you can use any other separator, for instance, a comma, a semi-colon, a hyphen, etc.
After that, we used the -f
operator (format operator), which acts like the +
operator and concatenates the values of $greetings
, $first_name
, $last_name
variables by placing them at index 0
, 1
, and 2
respectively. However, it differs from the +
operator because it allows us to have strings on specific indexes by changing the position of variables.
We can put a white space between each curly bracket pair to separate strings one from the other. The numbers wrapped around the curly brackets are the indexes. We can change the order of values without re-arranging variables as follows.
1 2 3 4 5 6 7 8 |
$greetings = "Hi" $first_name = "Mehvish" $last_name = "Ashiq" $result = "{2}{1}{0}" -f $greetings, $first_name, $last_name $result |
1 2 3 |
AshiqMehvishHi |
The third operator that we used was the -join
operator; it also works like +
and -f
operators; the good thing with the -join
operator is that we don’t have to write separator repeatedly, but we pass it once after -join
parameter.
We saved the results in $plus_without_separator
, $plus_with_separator
, $f_without_separator
, $f_with_separator
, $join_without_separator
, $join_with_separator
variables that we got after using +
, -f
, and -join
operators with and without a separator.
We can write these variables individually on the PowerShell console to display the output, but we adopted a different approach to have a look at all the outcomes simultaneously. For that, we used @{}
to create a hash table, which is a data structure with unique keys to specific values; we used it to have properties in key-value format.
Next, we enclosed the @{}
with $([ordered])
as $([ordered]@{})
to have properties in the order in which they are created; otherwise, they would be displayed randomly. See the following output to have a clear picture.
1 2 3 4 5 6 7 8 |
join_without_separator : HiMehvishAshiq join_with_separator : Hi Mehvish Ashiq f_without_separator : HiMehvishAshiq plus_with_separator : Hi Mehvish Ashiq f_with_separator : Hi Mehvish Ashiq plus_without_separator : HiMehvishAshiq |
At this point, we created an object with all the properties we defined using a hash table and saved it in the $props
variable. How? Let’s have a look at that. The New-Object
created an object while the -Property
parameter set every property value and invoked every method of the newly created object in the order in which they appeared in the hash table.
Remember, the hash table contains data in a key-value format where a key is the name of a method or property while the value is the method argument or property value. So now, the point is, why are we using the PSObject class?
We used the PSObject
class because if a new object was derived from the PSObject
class, and we write a property which doesn’t exist on that object, the New-Object
will add our specified property to our object as NoteProperty
. So for this particular situation, the New-Object
command will produce a non-terminating error if we don’t use the PSObject
class.
We saved our object’s data in the $result
array that we created using the array operator (@()
), and finally, we used the Write-Output
cmdlet to print the values of the specified variable on the PowerShell console.
Note that the Write-Output
will send the objects to a primary pipeline called Output Stream
or Success Pipeline
. This cmdlet is used to display objects and strings on the PowerShell console. For example, if we had an error, we would use Write-Error
to send error objects to an error pipeline.
Using Built-in Functions
To concatenate string in PowerShell:
- Declare and initialize three variables which contain string-type values.
- Use
concat()
,Join()
, andFormat()
functions to concatenate values of all variables we created in the first step.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$greetings = "Hi" $first_name = "Mehvish" $last_name = "Ashiq" $concat_without_separator = [String]::Concat($greetings, $first_name, $last_name) $concat_with_separator = [String]::Concat($greetings, " ", $first_name, " ", $last_name) $join_without_separator = [String]::Join("", $greetings, $first_name, $last_name) $join_with_separator = [String]::Join(" ", $greetings, $first_name, $last_name) $format_without_separator = [String]::Format("{0}{1}{2}", $greetings, $first_name, $last_name) $format_with_separator = [String]::Format("{0} {1} {2} ", $greetings, $first_name, $last_name) $props = $([ordered]@{ concat_without_separator=$concat_without_separator concat_with_separator=$concat_with_separator join_without_separator=$join_without_separator join_with_separator=$join_with_separator format_without_separator=$format_without_separator format_with_separator=$format_with_separator }) $results=@() $results += New-Object psobject -Property $props Write-Output $results |
1 2 3 4 5 6 7 8 |
concat_without_separator : HiMehvishAshiq concat_with_separator : Hi Mehvish Ashiq join_without_separator : HiMehvishAshiq join_with_separator : Hi Mehvish Ashiq format_without_separator : HiMehvishAshiq format_with_separator : Hi Mehvish Ashiq |
We’ve already learned this code in detail while using it with operators (+
, -f
, and -join
) in the [first section](add link of the first H2). This code example and its logic is the same as we used in the previous section except for one difference, we used built-in functions here.
In PowerShell, the built-in functions concat()
, Join()
, and Format()
can be used to concatenate strings where concat()
acts like +
operator, Join()
behaves like -join()
operator and Format()
acts like -f
operator.
There are only two advantages to using these methods. First, we have more organized code, and second, we don’t have any limit on the number of strings for concatenation.
Using Operators/Functions to Concatenate Strings with Numbers
To concatenate string in PowerShell:
- Declare and initialize two variables; one holds a numerical value, and the other contains a string type value.
- Use
+
,-f
, and-join
operators to concatenate values of all variables we created in the first step.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$count = "50" $product = "Snacks" $plus_without_separator = $count + $product $plus_with_separator = $product +" "+ $count $f_without_separator = "{0}{1}" -f $count, $product $f_with_separator = "{0} {1}" -f $product, $count $join_without_separator = $count, $product -join "" $join_with_separator = $product, $count -join " " $props = $([ordered]@{ plus_without_separator=$plus_without_separator plus_with_separator=$plus_with_separator f_without_separator=$f_without_separator f_with_separator=$f_with_separator join_without_separator=$join_without_separator join_with_separator=$join_with_separator }) $results=@() $results += New-Object psobject -Property $props Write-Output $results |
1 2 3 4 5 6 7 8 |
plus_without_separator : 50Snacks plus_with_separator : Snacks 50 f_without_separator : 50Snacks f_with_separator : Snacks 50 join_without_separator : 50Snacks join_with_separator : Snacks 50 |
We have already learned about the +
, -f
, and -join
operators, the logic of the above code and its working while using operators to concatenate strings in the first section.
Here, we used these operators to concatenate numerical values with string-type values. Note that there won’t be any error if we put integer type variables in first or last place while concatenating. We can use built-in functions as follows to concatenate strings with numerical values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$count = "50" $product = "Snacks" $concat_without_separator = [String]::Concat($count, $product) $concat_with_separator = [String]::Concat($product, " ", $count) $join_without_separator = [String]::Join("", $count, $product) $join_with_separator = [String]::Join(" ", $product, $count) $format_without_separator = [String]::Format("{0}{1}", $count, $product) $format_with_separator = [String]::Format("{0} {1}", $product, $count) $props = $([ordered]@{ concat_without_separator=$concat_without_separator concat_with_separator=$concat_with_separator join_without_separator=$join_without_separator join_with_separator=$join_with_separator format_without_separator=$format_without_separator format_with_separator=$format_with_separator }) $results=@() $results += New-Object psobject -Property $props Write-Output $results |
1 2 3 4 5 6 7 8 |
concat_without_separator : 50Snacks concat_with_separator : Snacks 50 join_without_separator : Snacks join_with_separator : Snacks 50 format_without_separator : 50Snacks format_with_separator : Snacks 50 |
Using StringBuilder
To concatenate String in PowerShell:
- Create a new object of type System.Text.StringBuilder.
- Use
append()
method to append String to it. - Use
ToString()
method to convert it back to String.
1 2 3 4 5 6 7 8 |
$first_name = "Mehvish" $last_name = "Ashiq" $Name = New-Object -TypeName System.Text.StringBuilder $temp_name = $Name.Append($first_name) $temp_name = $Name.Append($last_name) $Name.ToString() |
1 2 3 |
MehvishAshiq |
New-Object
cmdlet is used to create an instance of a Microsoft .NET Framework or COM object. We specified -TypeName
as System.Text.StringBuilder
to create StringBuilder
type of object.
We used append()
method to add $first_name
and $last_name
to the StringBuilder object and used ToString()
method to convert it to String.
That’s all about how to Concatenate String in PowerShell.