Updated March 4, 2023
Introduction to PowerShell join string
PowerShell Join String (method of a .NET string class) which combines multiple strings and generates a single string in the same order as the input was provided and the Join-String method has been added to the new PowerShell core version and it supports the advance Join method for the PowerShell String.
Syntax
-Join <String[]>
<String[]> -Join <Delimiter>
Join-String Syntax,
Join-String
[[-Property] <PSPropertyExpression>]
[[-Separator] <String>]
[-OutputPrefix <String>]
[-OutputSuffix <String>]
[-UseCulture]
[-InputObject <PSObject[]>]
[<CommonParameters>]
Join-Path syntax,
Join-Path
[-Path] <String[]>
[-ChildPath] <String>
[[-AdditionalChildPath] <String[]>]
[-Resolve]
[-Credential <PSCredential>]
[<CommonParameters>]
How does the PowerShell Join string works?
PowerShell Join command works on the String to concatenate the multiple strings into one. In the syntax of Join,
-Join <String[]>
<String[]> -Join <Delimiter>
String[] – Indicates the multiple string inputs.
<Delimiter> – Indicates the strings to join with. If no delimiter is provided, -Join command by default concatenate the string with (“ ”).
For example,
-Join ("Hello","PoweShell","Join")
Output:
You might have noticed that we have used string inside the brackets because the string with a round bracket mentions the array and if we don’t use it and if there is a comma (,) separator between them then only the first string will be submitted to the Join operator. See the example below,
-Join "Hello","PoweShell","Join"
Output:
You can also use the variable to concatenate the string.
$var = "Hello","PoweShell","Join"
-Join $var
Output:
This -Join String method works with any version of the PowerShell (Framework or Core version).
There is another method called Join-String which does the same multiple strings joining operations but it has some advanced methods like we can apply a formula for a substring, we can use splatting, use different parameters for separators after joining the script like SingleQuote or DoubleQuote or we can create a class definition from an object.
This method works only with PowerShell Core (6.2 or above version).
If you don’t specify any properties and use directly Join-String command, it will use $OFS (Output Field Separator ) and by default it is “ ”.
For example,
"Hello","PowerShell","Join" | Join-String
Output:
There is also the command Join-Path which joins the parent and the child’s path.
Examples
Here are the following examples mention below
1. Multiple ways to use the -Join string command.
Without using Array,
-Join "Animals","Birds","Vehicles"
Output:
With using an array,
-Join ("Animals","Birds","Vehicles")
Output:
With using a variable,
$a = "Hello","PowerShell","Join"
-Join $a
Output:
2. Join String with a specific delimiter.
To join a string with a specific delimiter, we need to use use the command as shown in the syntax and the example is shown below.
Example #1
"Hello","PowerShell","Join" -join ','
Output:
Example #2
"PowerShell","Azure","Runbook" -join " "
Output:
Example #3
In the below example, we are using characters to join the string.
"PowerShell","Azure","Runbook" -join "#O"
Output:
You can also directly apply the Join operation to the cmdlet. For example, the below command will get the content from a text file and joins them with a semicolon (;).
(Get-Content C:\Temp\Servers.txt) -join ';'
Output:
3. Join-Path command.
To join the child and parent path,
Join-Path -Path C:\ -ChildPath 'temp'
Output:
When we use the ‘\’ in the path that is ignored as shown below.
Join-Path -Path 'C:\' -ChildPath '\temp'
Output:
4. Join-String command.
As discussed above, this command provides advanced functionality to Join multiple strings. For example,
Example1: Default Separator of Join-String command.
"PowerShell","Azure","runbook" | Join-String
Output:
In the above example, strings are joined with $OFS.
Example2: Join-String command with separator.
"PowerShell","Azure","runbook" | Join-String -Separator ','
Output:
Example #3 – Join-String with a Separator and -SingleQuote property.
"PowerShell","Azure","runbook" | Join-String -Separator ',' -SingleQuote
Output:
Example #4 – Join-String with a Separator and -DoubleQuote property.
"PowerShell","Azure","runbook" | Join-String -Separator ',' -DoubleQuote
Output:
Example #5 – Get-Process with Join-String.
In the below example, we are joining the first 5 processes with the property “Name” with the separator and using the double quote.
Get-Process | Select -First 5 | Join-String -Property Name -Separator ',' -DoubleQuote
Output:
Here, the Get-Process which retrieves the list of the running processes is passed through the pipeline to the Join-String command, and Property “Name” is used to join. You can select different properties like process ID as well.
Get-Process | Select -First 5 | Join-String -Property id -Separator ','
Output:
Example #6 – Using a property name with SubString operation to Join Process names.
In the below example, we will use the substring operation to Join processes with the first 3 characters.
Get-Process | Select -First 5 | Join-String -Property {$_.Name.Substring(0,3)} -Separator ',' -DoubleQuote
Output:
In the below example, we are selecting the first 5 processes, passing it to the Name property of the Join-String command, and then converting it into Caps and joins.
Get-Process | Select -First 5 | Join-String -Property {$_.Name.ToUpper()} -Separator ',' -DoubleQuote
Output:
Example #7 – Separator with Special characters.
As a separator, we can also use special characters. A list of special characters you can get from the MS link below.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters?view=powershell-7.1
The below command will retrieve the processes starting with “Power” and join them with its “Name” property and separate them with a special character `n (New Line) and `t (tab) and labeled the output as “PowerShell Processes” and -OutPrefix parameter used for it.
Get-Process Power* | Join-String -Property Name -Separator "`n`t" -OutputPrefix "PowerShell Processes: `n`t"
Output:
Please note: We have added special characters at the end of the line so the first line will be formatted accordingly.
Conclusion
PowerShell Join commands are extremely helpful in the script when we are working with the strings and useful when we are working with paths so we can join the path and can also perform the join operation on the split string.
Recommended Articles
This is a guide to PowerShell join string. Here we discuss How does the PowerShell Join string works along with the examples and outputs. You may also have a look at the following articles to learn more –