Powershell append line to variable in for loop

powershell

Solution

Here are couple of ways to do this. Putting the lines in a single string:

$lines = ''
for ($i=0; $i -lt 10; $i++)
{
    $lines += "The current value of i is $i`n"
}
$lines

Or as an array of strings where each line is a different element in the array:

$lines = @()
for ($i=0; $i -lt 10; $i++)
{
    $lines += "The current value of i is $i"
}
$lines

Problem

I have a foreach loop and use the write-host cmdlet to write to the console. I now wish to write the lines into a variable that will store all of the result from the loop. What is the cmdlet/syntax for this?

Original source