How do I concatenate strings and variables in PowerShell?

PowershellString Concatenation

Powershell Problem Overview


Suppose I have the following snippet:

$assoc = New-Object PSObject -Property @{
    Id = 42
    Name = "Slim Shady"
    Owner = "Eminem"
}

Write-Host $assoc.Id + "  -  "  + $assoc.Name + "  -  " + $assoc.Owner

I'd expect this snippet to show:

> 42 - Slim Shady - Eminem

But instead it shows:

> 42 + - + Slim Shady + - + Eminem

Which makes me think the + operator isn't appropriate for concatenating strings and variables.

How should you approach this with PowerShell?

Powershell Solutions


Solution 1 - Powershell

Write-Host "$($assoc.Id) - $($assoc.Name) - $($assoc.Owner)"

See the Windows PowerShell Language Specification Version 3.0, p34, sub-expressions expansion.

Solution 2 - Powershell

There is a difference between single and double quotes. (I am using PowerShell 4).

You can do this (as Benjamin said):

$name = 'Slim Shady'
Write-Host 'My name is'$name
-> My name is Slim Shady

Or you can do this:

$name = 'Slim Shady'
Write-Host "My name is $name"
-> My name is Slim Shady

The single quotes are for literal, output the string exactly like this, please. The double quotes are for when you want some pre-processing done (such as variables, special characters, etc.)

So:

$name = "Marshall Bruce Mathers III"
Write-Host "$name"
-> Marshall Bruce Mathers III

Whereas:

$name = "Marshall Bruce Mathers III"
Write-Host '$name'
-> $name

(I find How-to: Escape characters, Delimiters and Quotes good for reference).

Solution 3 - Powershell

You can also use -join

E.g.

$var = -join("Hello", " ", "world");

Would assign "Hello world" to $var.

So to output, in one line:

Write-Host (-join("Hello", " ", "world"))

Solution 4 - Powershell

One way is:

Write-Host "$($assoc.Id)  -  $($assoc.Name)  -  $($assoc.Owner)"

Another one is:

Write-Host  ("{0}  -  {1}  -  {2}" -f $assoc.Id,$assoc.Name,$assoc.Owner )

Or just (but I don't like it ;) ):

Write-Host $assoc.Id  "  -  "   $assoc.Name  "  -  "  $assoc.Owner

Solution 5 - Powershell

Try wrapping whatever you want to print out in parentheses:

Write-Host ($assoc.Id + "  -  "  + $assoc.Name + "  -  " + $assoc.Owner)

Your code is being interpreted as many parameters being passed to Write-Host. Wrapping it up inside parentheses will concatenate the values and then pass the resulting value as a single parameter.

Solution 6 - Powershell

Another option is:

$string = $assoc.ID
$string += " - "
$string += $assoc.Name
$string += " - "
$string += $assoc.Owner
Write-Host $string

The "best" method is probably the one C.B. suggested:

Write-Host "$($assoc.Id)  -  $($assoc.Name)  -  $($assoc.Owner)"

Solution 7 - Powershell

While expression:

"string1" + "string2" + "string3"

will concatenate the string, you need to put a $ in front of the parenthesis to make it evaluate as a single argument when passed to a PowerShell command. Example:

Write-Host $( "string1" + "string2" + "string3" )

As a bonus, if you want it to span multiple lines, then you need to use the awkward backtick syntax at the end of the line (without any spaces or characters to the right of the backtick).

Example:

Write-Host $(`
    "The rain in "        +`
    "Spain falls mainly " +`
    "in the plains"       )`
    -ForegroundColor Yellow

(Actually, I think PowerShell is currently implemented a little bit wrong by requiring unnecessary backticks between parentheses. If Microsoft would just follow Python or Tcl parenthesis rules of allowing you to put as many newlines as you want between the starting and ending parenthesis then they would solve most of the problems that people don't like about PowerShell related to line continuation, and concatenation of strings.

I've found that you can leave the backticks off sometimes on line continuations between parenthesis, but it's really flaky and unpredictable if it will work... It's better to just add the backticks.)

Solution 8 - Powershell

You need to place the expression in parentheses to stop them being treated as different parameters to the cmdlet:

Write-Host ($assoc.Id + "  -  "  + $assoc.Name + "  -  " + $assoc.Owner)

Solution 9 - Powershell

(Current PowerShell version 5.1.17134.407)

This also works as of now:

$myVar = "Hello"

echo "${myVar} World"

Note: this only works with double quotes

Solution 10 - Powershell

Here is another way as an alternative:

Write-Host (" {0}  -  {1}  -  {2}" -f $assoc.Id, $assoc.Name, $assoc.Owner)

Solution 11 - Powershell

I just want to bring another way to do this using .NET String.Format:

$name = "Slim Shady"
Write-Host ([string]::Format("My name is {0}", $name))

Solution 12 - Powershell

I seem to struggle with this (and many other unintuitive things) every time I use PowerShell after time away from it, so I now opt for:

[string]::Concat("There are ", $count, " items in the list")

Solution 13 - Powershell

These answers all seem very complicated. If you are using this in a PowerShell script you can simply do this:

$name = 'Slim Shady'
Write-Host 'My name is'$name

It will output

> My name is Slim Shady

Note how a space is put between the words for you

Solution 14 - Powershell

Concatenate strings just like in the DOS days. This is a big deal for logging so here you go:

$strDate = Get-Date
$strday = "$($strDate.Year)$($strDate.Month)$($strDate.Day)"

Write-Output "$($strDate.Year)$($strDate.Month)$($strDate.Day)"
Write-Output $strday

Solution 15 - Powershell

From What To Do / Not to Do in PowerShell: Part 1:

$id = $assoc.Id
$name = $assoc.Name
$owner = $assoc.owner
"$id - $name - $owner"

Solution 16 - Powershell

Write-Host can concatenate like this too:

Write-Host $assoc.Id" - "$assoc.Name" - "$assoc.Owner

This is the simplest way, IMHO.

Solution 17 - Powershell

If you're concatenating strings to build file paths, use the Join-Path command:

Join-Path C:\temp "MyNewFolder"

It'll automatically add the appropriate trailing / leading slashes for you, which makes things a lot easier.

Solution 18 - Powershell

$assoc = @{
    Id = 34
    FirstName = "John"
    LastName = "Doe"
    Owner = "Wife"
}

$assocId = $assoc.Id
$assocFN = $assoc.FirstName
$assocLN = $assoc.LastName
$assocName = $assocFN, $assocLN -Join " "
$assocOwner = $assoc.Owner

$assocJoin = $assocId, $assocName, $assocOwner -join " - "
$assocJoin
#Output = 34 - John Doe - Wife

Solution 19 - Powershell

Personally I prefer this style:

[string]::Join(' - ', 42, 'Slim Shady', 'Eminem')

or based on the above (unordered) object:

[string]::Join(' - ',$assoc.psObject.Properties.value)

Solution 20 - Powershell

Just for the fun. You can also access the values of the PSObject directly like below:

$assoc.psobject.Properties.value -join " - "

But if you do not specify that the object should be ordered, PowerShell will display the values in a random order. So you should add the flag [ordered]:

$assoc = [pscustomobject] [ordered] @{
    Id = 42
    Name = "Slim Shady"
    Owner = "Eminem"
}

Solution 21 - Powershell

You can also get access to C#/.NET methods, and the following also works:

$string1 = "Slim Shady, "
$string2 = "The real slim shady"

$concatString = [System.String]::Concat($string1, $string2)

Output:

Slim Shady, The real slim shady

Solution 22 - Powershell

As noted elsewhere, you can use join.

If you are using commands as inputs (as I was), use the following syntax:

-join($(Command1), "," , $(Command2))

This would result in the two outputs separated by a comma.

See https://stackoverflow.com/a/34720515/11012871 for related comment

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNinja CowgirlView Question on Stackoverflow
Solution 1 - PowershellDavid BrabantView Answer on Stackoverflow
Solution 2 - PowershellTinyRacoonView Answer on Stackoverflow
Solution 3 - PowershellmanywaysView Answer on Stackoverflow
Solution 4 - PowershellCB.View Answer on Stackoverflow
Solution 5 - PowershellgoricView Answer on Stackoverflow
Solution 6 - PowershellFrode F.View Answer on Stackoverflow
Solution 7 - PowershellBimoView Answer on Stackoverflow
Solution 8 - PowershellRichardView Answer on Stackoverflow
Solution 9 - PowershellBenjaminView Answer on Stackoverflow
Solution 10 - PowershellDave SextonView Answer on Stackoverflow
Solution 11 - PowershellMartin BrandlView Answer on Stackoverflow
Solution 12 - PowershellLuke PuplettView Answer on Stackoverflow
Solution 13 - Powershellelev8edView Answer on Stackoverflow
Solution 14 - PowershellKelly DavisView Answer on Stackoverflow
Solution 15 - PowershellE.V.I.L.View Answer on Stackoverflow
Solution 16 - PowershellMordechaiView Answer on Stackoverflow
Solution 17 - PowershellsquiccView Answer on Stackoverflow
Solution 18 - Powershellblayderunner123View Answer on Stackoverflow
Solution 19 - PowershellCarstenView Answer on Stackoverflow
Solution 20 - PowershellR.AmersfoortView Answer on Stackoverflow
Solution 21 - PowershellIan RobertsonView Answer on Stackoverflow
Solution 22 - PowershellS. MeltedView Answer on Stackoverflow