Search code examples
powershellpowershell-2.0

Is there a string concatenation shortcut in PowerShell?


Like with numerics?

For example,

$string = "Hello"
$string =+ " there"

In Perl you can do

my $string = "hello"
$string .= " there"

It seems a bit verbose to have to do

$string = $string + " there"

or

$string = "$string there"

Solution

  • You actually have the operator backwards. It should be +=, not =+:

    $string = "Hello"
    $string += " there"
    

    Below is a demonstration:

    PS > $string = "Hello"
    PS > $string
    Hello
    PS > $string += " there"
    PS > $string
    Hello there
    PS >
    

    However, that is about the quickest/shortest solution you can get to do what you want.