简体   繁体   中英

Powershell append line to variable in for loop

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM