Table of Contents
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
- concatenate string in loop powershell
- concatenate two strings in powershell script
- concatenate strings in array powershell
- concatenate string and date in powershell
- concatenate string and integer in powershell
- powershell concatenate string in select-object
- concatenate a string in powershell
- powershell concatenate string in argument