主题
    脚本块

简短说明
    在 Windows PowerShell 中将多个语句和命令分组为单个代码块

详细说明
    以下为一些脚本块示例：

    此示例将循环遍历每个扩展名为 *.txt 的文件，然后将其全名和长度写入主机。

        foreach ($file in Get-ChildItem c:\techdocs\*.txt)
        {
            Write-Host $file.fullname $file.length
        }

    此示例将 if 语句嵌套在 foreach 循环中。

        foreach ($file in Get-ChildItem c:\techdocs\*.txt)
        {
            if ($file.length -ge 100)
            {
                Write-Host $file.name - $file.length bytes
            }
            else
            {
                Write-Host $file.name - "该文件小于 100 个字节。"
            }
        }



将脚本块赋予变量
    还可以按如下所示将脚本块赋予变量：

        $a = {Write-Host $file.fullname $file.length}

    按如下所示使用 & 执行脚本块变量：



        foreach ($file in Get-ChildItem c:\techdocs\*.txt) {&$a}

        Write-Host $file.fullname $file.length



函数和筛选器

        function text_files
        {
            Get-ChildItem c:\techdocs\*.txt
        }



另请参阅
    有关流控制语句的信息，请在 PowerShell 命令提示符下输入以下命令：

        help about_flow_control

    有关函数和筛选器的信息，请输入以下命令：

        help about_function

    有关作用域的信息，请输入以下命令：

        help about_scope


