虚位以待(AD)
虚位以待(AD)
首页 > 脚本专栏 > Powershell > Windows Powershell Foreach 循环

Windows Powershell Foreach 循环
类别:Powershell   作者:码皇   来源:互联网   点击:

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

相关热词搜索: Windows Powershell Foreach 循环