0

In Powershell, how can I join 5 strings into one string.

 $s1='This`-string
 $s2='is' -string
 $s3='a'  -string
 $s4='good' -string
 $s5='thing' -string
 $s6=$s1+$s2+$s3+$s4+$s5
 write-host"result is "$s6-->Thisisagoodthing 
Anthon
  • 69,918
  • 32
  • 186
  • 246
  • Please mention your issue properly with details.Question seems broken. – Satyam Koyani Apr 03 '15 at 06:27
  • 1
    Possibly duplicate of [How to concatenate strings and variables in Powershell?](http://stackoverflow.com/questions/15113413/how-to-concatenate-strings-and-variables-in-powershell) – mins Apr 03 '15 at 06:41
  • What's wrong with the current answers? None is selected, but both seem correct. – mins Apr 25 '15 at 13:51

2 Answers2

3

You might use :

$s6="$s1$s2$s3$s4$s5"

Write-Host "result is $s6"

Community
  • 1
  • 1
  • For displaying purpose it is working but I want the entire result in a string variable..which is not working.Can you please check it once and revert to me. – chaitanya chaitanya Apr 03 '15 at 07:20
  • @Chaitanya chaitanya your are wrong $s6 is a string var. This is the good answer or I don't understand your question ? – JPBlanc Apr 03 '15 at 07:28
1

You can literally "join" your strings with the -join operator:

$s6 = $s1,$s2,$s3,$s4,$s5 -join ""

Or, without specifying a delimiter:

$s6 = -join @($s1,$s2,$s3,$s4,$s5)
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206