Windows Powershell Foreach 循环

Eleanor ·
更新时间:2024-09-20
· 777 次阅读

下面举两个例子:

代码如下:
$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 IF-ELSEIF-ELSE 语句Windows Powershell Switch 语句Windows Powershell ForEach-Object 循环Windows Powershell Do While 循环Windows Powershell For 循环Windows Powershell Switch 循环



foreach 循环 windows PowerShell

需要 登录 后方可回复, 如果你还没有账号请 注册新账号