Low Orbit Flux Logo 2 F

PowerShell How To Append To A Variable

Appending to a variable is pretty easy to do in PowerShell. You can do this with a regular variable that holds a string. This will basically just append to the string. You can also do this with an array which will append a new element to the array. This becomes especially useful when working with loops.

Append to a string variable:


$not_edible = 'sand'
$not_edible += ' and gravel'
$not_edible

Append to a an array variable:


$edible = @('orange','banana','apple')
$edible += 'kiwi'
$edible

Append using a loop:


$z += "Starting value $i"

$z = @()
for ( $c=0; $c -lt 10; $c++ )
{
    $z += "Next value: $i"
}

$z
$z[5]