You can concat strings in PowerShell by using the + operator, but like in C#, I think it is better to use a formatting approach by using the –f string operator:

 

[-f operator]

PS C:\Users\rLisdonk> "My computer {0} has {1} MB of memory." -f "L001", "4096"
My computer L001 has 4096 MB of memory.

 

[Double quoted string with variables]

You could also use variables in the double quoted string, like:

$myComputer = “L001”

$memory = “4096”

PS C:\Users\rLisdonk> "My computer $myComputer has $memory MB of memory."

 

[+= operator]

You could also use += operator to concat strings

PS C:\Users\rLisdonk> $test = "This is a "
PS C:\Users\rLisdonk> $test += "test string"
PS C:\Users\rLisdonk> $test
This is a test string

 

[Escape characters]

see http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences

Tags:

Leave a Reply