Does PowerShell require the opening curly brace of a script block to be on the same line?

前端 未结 2 1588
一个人的身影
一个人的身影 2021-01-13 20:25

Okay, the expected \"output\" of this script is fairly obvious, and it works. It copies files:

Get-ChildItem -Recurse -Directory | ForEach-Object{
    if ($         


        
2条回答
  •  耶瑟儿~
    2021-01-13 20:52

    PS C:\> Get-Help -Name 'ForEach-Object'
    
    SYNTAX
        ForEach-Object [-MemberName]  [-ArgumentList ] [-Confirm] [-InputObject ] [-WhatIf] []
    
        ForEach-Object [-Process]  [-Begin ] [-Confirm] [-End ] [-InputObject ] [-RemainingScripts ] [-WhatIf] []
    

    By using a script block, you're using positionally-bound parameters of the ForEach-Object cmdlet. In this case, the -Process { } parameter set. If you escape the end of the line (in essence, escaping the newline), you can get around that, but it's generally a bad practice.

提交回复
热议问题