当前位置: 移动技术网 > IT编程>脚本编程>Shell > Windows Powershell Foreach 循环

Windows Powershell Foreach 循环

2017年12月08日  | 移动技术网IT编程  | 我要评论

下面举两个例子:

复制代码 代码如下:

$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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网