How to concatenate string in powershell?

Clear2all Professional Blog new

How to concatenate string in powershell?

 

1. Concatenate string Use -join

$var = -join(“Hello”, ” “, “world”);

Stores  “Hello world” to $var.

So to output, in one line:

Write-Host (-join(“Hello”, ” “, “world”))

2. Concatenate string using “-“

Write-Host “$($products.Id) – $($products.Name) – $($products.Owner)”

3. Concatenate string using indirectly with “-“

Write-Host (“{0} – {1} – {2}” -f $products.Id,$products.Name,$products.Owner )
Or just (but I don’t like it 😉 ):

Write-Host $products.Id ” – ” $products.Name ” – ” $products.Owner

 

4. Concatenate string using “+”

$string = $products.ID
$string += ” – ”
$string += $products.Name
$string += ” – ”
$string += $products.Owner
Write-Host $string

1. Best easy method to Concatenate string

Write-Host “$($products.Id) – $($products.Name) – $($products.Owner)”

 

I will add more ways for below soon and comment you would like to be soon rather than later

 

  1. concatenate string in loop powershell
  2. concatenate two strings in powershell script
  3. concatenate strings in array powershell
  4. concatenate string and date in powershell
  5. concatenate string and integer in powershell
  6. powershell concatenate string in select-object
  7. concatenate a string in powershell
  8. powershell concatenate string in argument

Leave a Reply

Your email address will not be published. Required fields are marked *