Updated March 6, 2023
Introduction to PowerShell Join
The join cmdlet is used to join multiple strings into a single string. The order in which the multiple strings are retained during the concatenation operation is the same order in which they are passed to the cmdlet. Join cmdlet is also used to covert text that is present in pipeline objects into a single string value. This article will explain in detail about join cmdlet in PowerShell, its syntax and usage along with appropriate examples.
Syntax:
The basic syntax of the join operator is as follows
-Join <String1[]> <String2[]> <String2[]> -Join <Delimiter>
Where string1, string2 and string3 represent the various strings that needs to be merged. The delimiter represents the character that should be present between the strings that are concatenated. If no value is specified, the “” is used by PowerShell.
The following are the other available syntax
Join-String [[-Property] <PSPropertyExpression>] [[-Separator] <String>] [-OutputPrefix <String>] [-OutputSuffix <String>] [-UseCulture] [-InputObject <PSObject[]>] [<CommonParameters>]
Join-String [[-Property] <PSPropertyExpression>] [[-Separator] <String>] [-OutputPrefix <String>] [-OutputSuffix <String>] [-SingleQuote] [-UseCulture] [-InputObject <PSObject[]>] [<CommonParameters>]
Join-String [[-Property] <PSPropertyExpression>] [[-Separator] <String>] [-OutputPrefix <String>] [-OutputSuffix <String>] [-DoubleQuote] [-UseCulture] [-InputObject <PSObject[]>] [<CommonParameters>]
Join-String [[-Property] <PSPropertyExpression>] [[-Separator] <String>] [-OutputPrefix <String>] [-OutputSuffix <String>] [-FormatString <String>] [-UseCulture] [-InputObject <PSObject[]>] [<CommonParameters>]
For the Join-String cmdlet, the default separator that is used by PowerShell is $OFS if the user doesn’t specify any value. If a property name is specified, then that property’s value can be converted to a string and subsequently concatenated to a string. A script block can also be used in place of a property name. If that is done, then the script block’s result is converted to a string before concatenation. This cmdlet is the latest and was released as part of PowerShell version 6.2
When the comma has used a delimiter with the join operator, the join operator is given a higher priority. In that case, only the first string is considered, in order to avoid that the strings must be enclosed in parentheses.
Example:
Input:
$test = "one", "two", "three", "four","five"
-join $test
Output:
PS C:\WINDOWS\system32> $test = "one", "two", "three", "four","five"
-join $test
onetwothreefourfive
Parameters:
- DoubleQuote:
This parameter is used to encapsulate each pipeline objects string value inside double quotes. The datatype of this parameter is switch and its default value is false. This parameter doesn’t accept pipeline input and wildcard characters are also not accepted.
- FormatString:
This denotes the format structure of the item. The datatype of this parameter is string. None is the default value of this parameter. This parameter doesn’t accept pipeline input and wildcard characters are also not accepted. This is an optional parameter.
- InputObject:
This denotes the input texts that are to be joined. It can either be a variable or a command object. The datatype of this parameter is PSObject[]. This parameter’s default value is none. This parameter accepts pipeline input whereas wildcard characters are not allowed. This is an optional parameter.
- OutputPrefix:
This denotes the text that will be inserted before the result. It can contain special characters such as newline or a tab. The datatype of this parameter is string. It can be referred using its alias, op. None is the default value of this parameter. This parameter doesn’t accept pipeline input and wildcard characters are also not accepted. This is an optional parameter.
- OutputSuffix:
This denotes the text that will be inserted after the result. . It can contain special characters such as newline or a tab. The datatype of this parameter is string. It can be referred using its alias, os. None is the default value of this parameter. This parameter doesn’t accept pipeline input and wildcard characters are also not accepted. This is an optional parameter.
- Property:
This denotes the object that will convert the pipeline object to a string. The datatype of this parameter is PSPropertyExpression. This parameter is placed at zeroth position. None is its default value. Both pipeline input and wild card characters aren’t accepted. This is a mandatory parameter.
- Separator:
This denotes the character that needs to be inserted between the text that are joined from the pipeline object. It is generally a comma(,) or a semicolon (; ). It is placed at the number one position. None is its default value. Both pipeline input and wild card characters aren’t accepted. This is a mandatory parameter.
- SingleQuote:
This parameter is used to wrap the output string value from pipeline object inside single quote. Its datatype is switch. None is its default value. Both pipeline input and wild card characters aren’t accepted. This is an optional parameter.
- UseCulture:
This uses the current culture’s separator as the value of the item delimiter. To find this information, Get-Culture).TextInfo.ListSeparator is used. The datatype of this parameter is switch. None is its default value. Both pipeline input and wild card characters aren’t accepted. This is an optional parameter.
Example:
Write-Host "Welcome to powershell join example"
Write-Host "Demo of normal join"
$stringa="vignesh"
$stringb="Krishnakumar"
-join($stringa, $stringb)
Write-Host "Joining use a delimiter" -ForegroundColor Green
$stringa,$stringb -join " "
Write-Host "Joining using the semicolon delimiter"
$stringa,$stringb -join ";"
Write-Host "removing numbers from a string and join them using semi-colon" -ForegroundColor Green
$test="This123is67Vignesh5678Working15466From89Chennai"
Write-Host "Numbers are removed" -ForegroundColor Green
$test -split '\d+' -join ";"
Write-Host "Demo of addding a text to each string" -ForegroundColor Green
$test="Iam vig","from chennai","am a ","freelancer"
Write-Host "actual text is" $test
Write-Host "Stars are appended to the actual test" -ForegroundColor Green
$test -join "***"
Write-Host "Joining multi line strings" -ForegroundColor Green
$stringa=@"
Iam karthik
am 12 years
old
"@
$stringb=@"
Powershell is a
coll language
to learn
"@
-join ($stringa , $stringb)
Write-Host "Inserting a new new line before the join" -ForegroundColor Green
$stringa,$stringb -join "`n"
Output:
Conclusion
Thus, the article explained in detail PowerShell and how can it be achieved using various methods. The article also explained the various parameters that are associated with the join command and how they can be used to achieve different functionalities. To learn more in detail it is advisable to write and practice sample scripts.
Recommended Articles
This is a guide to PowerShell join. Here we discuss how can PowerShell join achieved using various methods and also explained the various parameters. You may also look at the following article to learn more –