Collections:
Other Resources:
String Concatenation in Windows PowerShell
How to concatenate strings in Windows PowerShell? I want to join two strings together.
✍: FYIcenter.com
The easiest way to join two strings together is to use the concatenate operator (+)
as shown in the example below:
PS C:\fyicenter> $name = "John" PS C:\fyicenter> # using string concatenation operation PS C:\fyicenter> $msg = "Hello "+$name+"!" PS C:\fyicenter> Write-Host $msg Hello John!
Another way to join multiple strings together is the "-Join" operation, which takes an array of strings and returns a single string with array elements joined together. See the example below:
PS C:\fyicenter> $name = "John"
PS C:\fyicenter> # using -Join operation
PS C:\fyicenter> $msg = -Join ("Hello ", $name, "!")
PS C:\fyicenter> Write-Host $msg
Hello John!
If you are .NET developer, you probably want to use the System.Text.StringBuilder class to build a long string with smaller pieces as shown in the example below:
PS C:\fyicenter> $name = "John"
PS C:\fyicenter> $str = new-object System.Text.StringBuilder
PS C:\fyicenter> # appending another string to the end of a string
PS C:\fyicenter> $str = $str.Append("Hello ")
PS C:\fyicenter> $str = $str.Append($name)
PS C:\fyicenter> $str = $str.Append("!")
PS C:\fyicenter> $msg = $str.ToString()
PS C:\fyicenter> Write-Host $msg
Hello John!
⇒ Variable Expansion in Strings in Windows PowerShell
⇐ What Is String in Windows PowerShell
2016-10-20, ∼2869🔥, 0💬
Popular Posts:
Where to find tutorials on using Windows Server 2012? I want to learn how to use Windows Server 2012...
What files are stored in the "C:\Users\<userid >\AppData\Local\M icrosoft\Windows\Tempor. ..
How to install Windows 7 Service Pack 1 (SP1)? I have the SP1 file downloaded. If you have the Windo...
What is "Cryptographic Services" in my Windows XP service list? And how is "Cryptographic Services" ...
What is "UPnP Device Host" in my Windows 7 service list? And how is "UPnP Device Host" service relat...