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]



1 Comment on To combine, format, join or concat strings in PowerShell

  1. Bogdan says:

    Nice info, do you know how to create a somethig like :
    $list = “1”, “2”, “3”
    $test = “changing value” -f “constant”

    from a txt file like this:
    1, changing value a1
    2, changing value a2
    and so on.

Leave a Reply