Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell append line to variable in for loop

Tags:

powershell

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?

like image 269
meeeeeeeeee Avatar asked Oct 18 '12 15:10

meeeeeeeeee


1 Answers

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
like image 144
Keith Hill Avatar answered Oct 01 '22 09:10

Keith Hill