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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With