Powershell Foreach 循环 6


Foreach-object 为cmdlet命令,使用在管道中,对管道结果逐个处理,foreach为遍历集合的关键字。
下面举两个例子:

$array=7..10
foreach ($n in $array)
{
    $n*$n
}

#49
#64
#81
#100

foreach($file in dir c:\windows)
{
    if($file.Length -gt 1mb)
    {
        $File.Name
    }
}

#explorer.exe
#WindowsUpdate.log

这里只为了演示foreach,其实上面的第二个例子可以用Foreach-Object更简洁。

PS C:\Powershell> dir C:\Windows | where {$_.length -gt 1mb} |foreach-object {$_.Name}
explorer.exe
WindowsUpdate.log
本文链接: https://www.pstips.net/powershell-foreach-loop.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

回复 Mooser Lee 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

6 条评论 “Powershell Foreach 循环