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 
3
  • Please mention your issue properly with details.Question seems broken. Commented Apr 3, 2015 at 6:27
  • 1
    Possibly duplicate of How to concatenate strings and variables in Powershell? Commented Apr 3, 2015 at 6:41
  • What's wrong with the current answers? None is selected, but both seem correct. Commented Apr 25, 2015 at 13:51

2 Answers 2

4

You might use :

> $s6="$s1$s2$s3$s4$s5"
>
> Write-Host "result is $s6"
Sign up to request clarification or add additional context in comments.

2 Comments

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 your are wrong $s6 is a string var. This is the good answer or I don't understand your question ?
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.