1

I am new to powershell language and I have problems understanding some basic concepts regarding string concatenation.

I tried to concat a string with the + char as I knew it from other programming languages i. e. Java.

line 1: $result = 7
line 2: Write-Host "Result: " + $result + "!" # Result:  + 7 + !

I then realized (i. e. in this question How do I concatenate strings and variables in PowerShell?) that I need to do it (in one of) the powershell way(s); for example like this.

line 3: Write-Host "Result: $result!" # Result: 7!

As I experimented a little I found out that if I assign the expression in line 2 to a variable it somehow works as I anticipated it in the first place.

line 4: $str = "Result: " + $result + "!"
line 5: Write-Host $str # Result: 7!

So my question is, why is there a difference if I pass a Java-style concatenated string to Write-Output cmdlet or if I assign the same string to a variable?

Filou
  • 490
  • 4
  • 17
  • Compare `Write-Host 2 + 3 + 4`, `Write-Host (2 + 3 + 4)`, `Write-Host "(2 + 3 + 4)"`. PowerShell's parsing is peculiar like that to accommodate the "usual" way of passing parameters to commands. If you want an expression, you'll have to force it. – Jeroen Mostert Nov 27 '17 at 14:57
  • @JeroenMostert That's not an oddity of PowerShell parsing, but the `Write-Host` cmdlet. – Maximilian Burszley Nov 27 '17 at 16:01
  • @TheIncorrigible1: `cmd /c echo 1 + 2 + 3`, `cmd /c echo (1 + 2 + 3)`. And no, it's not an oddity of `cmd` or `echo` either. – Jeroen Mostert Nov 27 '17 at 16:08
  • @JeroenMostert We're talking about PowerShell, not cmd. – Maximilian Burszley Nov 27 '17 at 16:14
  • @TheIncorrigible1: I mean *executing* those commands in PowerShell, obviously. One passes `/c echo 1 + 2 + 3` to `cmd`, the other `/c echo 6`. – Jeroen Mostert Nov 27 '17 at 16:15
  • @JeroenMostert If you need to execute `cmd /c`, just create a batch file.. PowerShell evaluates `1+2+3 = (1+2+3)` equally. – Maximilian Burszley Nov 27 '17 at 16:16
  • @TheIncorrigible1: We're talking past each other. PowerShell evaluates `1 + 2 + 3` and `(1 + 2 + 3)` equally *if both are expressions*. When `1 + 2 + 3` is being parsed as arguments, you get 5 arguments. (`1+2+3` is only one argument.) This is true regardless of whether native cmdlets or external commands are used, is my point. `cmd` was just to illustrate that, it's immaterial to the point made. – Jeroen Mostert Nov 27 '17 at 16:21
  • @JeroenMostert That's not entirely true. In your `Write-Host` example, it's trying to interpret whatever you pass it into a single object. Ordinarily, yes, it does see spaces as separating arguments, which is why you'd get a different response with parens since that's a grouping operator. – Maximilian Burszley Nov 27 '17 at 16:23

1 Answers1

0

String concatenation and expansion is a bit different in PowerShell, here are several ways to accomplish it-


Format operator:

PS C:\> 'This exhibits {0} string expansions! {1} {0}!' -f @(2,'Wow!')
This exhibits 2 string expansions! Wow! 2!

Each item in the array is accessed by the number in the braces.

Subexpression:

PS C:\> "Sometimes you need calculations in a string. 5 + 3 = $(5 + 3)"
Sometimes you need calculations in a string. 5 + 3 = 8

An explanation here: this will not work with string literals, i.e., ''. Everything contained in the subexpression will be evaluated and converted to a string if possible utilizing an object's ToString() method.

String Expansion:

 PS C:\> $Var = 'This string'
 PS C:\> "$Var is amazing!"
 This string is amazing!

This will also not work with string literals, i.e., ''. If you need to concatenate a variable-qualifying character next to the variable call, you can use curly braces to avoid a null value, i.e., ${Var}_notattached

String Concatenation:

Tried and true:

PS C:\> 'Sometimes you just ' + 'need to add. 8 = ' + 8
Sometimes you just need to add. 8 = 8
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • Hi @TheIncorrigible1, first of all thanks for your answer. I am aware how to _do_ the string concatenation in different ways as you described above. But my question was _why_ is there a different interpretation between assignment and parameter for a cmdlet? Take your very last example. If you write a `Write-Host` (or a `Write-Output` that takes every blank as a separation for an array) in front of your string, there will be a completely different result. *Why?* – Filou Nov 28 '17 at 06:25
  • @Filou Adjust your question / title to reflect that. – Maximilian Burszley Nov 28 '17 at 06:26