0 Comments

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

One Reply to “To combine, format, join or concat strings in PowerShell”

  1. Hy
    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

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts