简体   繁体   English

Powershell将for行添加到for循环中的变量

[英]Powershell append line to variable in for loop

I have a foreach loop and use the write-host cmdlet to write to the console. 我有一个foreach循环并使用write-host cmdlet写入控制台。 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? 什么是cmdlet /语法?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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