How do you comment out code in PowerShell?

前端 未结 10 1599
傲寒
傲寒 2020-12-12 08:59

How do you comment out code in PowerShell (1.0 or 2.0)?

10条回答
  •  清歌不尽
    2020-12-12 09:32

    I'm a little bit late to this party but seems that nobody actually wrote all use cases. So...

    Only supported version of PowerShell these days (fall of 2020 and beyond) are:

    • Windows PowerShell 5.1.x
    • PowerShell 7.0.x.

    You don't want to or you shouldn't work with different versions of PowerShell.

    Both versions (or any another version which you could come around WPS 3.0-5.0, PS Core 6.x.x on some outdated stations) share the same comment functionality.

    One line comments

    # Get all Windows Service processes <-- one line comment, it starts with '#'
    Get-Process -Name *host*
    
    Get-Process -Name *host* ## You could put as many ### as you want, it does not matter
    
    Get-Process -Name *host* # | Stop-Service # Everything from the first # until end of the line is treated as comment
    
    Stop-Service -DisplayName Windows*Update # -WhatIf # You can use it to comment out cmdlet switches
    

    Multi line comments

    <#
    Everyting between '< #' and '# >' is 
    treated as a comment. A typical use case is for help, see below.
    
    # You could also have a single line comment inside the multi line comment block.
    # Or two... :)
    
    #>
    
    <#
    .SYNOPSIS
        A brief description of the function or script.
        This keyword can be used only once in each topic.
    
    .DESCRIPTION
        A detailed description of the function or script.
        This keyword can be used only once in each topic.
    
    .NOTES
        Some additional notes. This keyword can be used only once in each topic.
        This keyword can be used only once in each topic.
    
    .LINK
        A link used when Get-Help with a switch -OnLine is used.
        This keyword can be used only once in each topic.
    
    .EXAMPLE
        Example 1
        You can use this keyword as many as you want.
    
    .EXAMPLE
        Example 2
        You can use this keyword as many as you want.
    #>
    

    Nested multi line comments

    <#
    Nope, these are not allowed in PowerShell.
    
    <# This will break your first multiline comment block... #>
    ...and this will throw a syntax error.
    #>
    

    In code nested multi line comments

    <# 
    The multi line comment opening/close
    can be also used to comment some nested code
    or as an explanation for multi chained operations..
    #>
    Get-Service | <# Step explanation #>
    Where-Object { $_.Status -eq [ServiceProcess.ServiceControllerStatus]::Stopped } | 
    <# Format-Table -Property DisplayName, Status -AutoSize |#>
    Out-File -FilePath Services.txt -Encoding Unicode
    

    Edge case scenario

    # Some well written script
    exit
    Writing something after exit is possible but not recommended.
    It isn't a comment.
    Especially in Visual Studio Code, these words baffle PSScriptAnalyzer.
    You could actively break your session in VS Code.
    

提交回复
热议问题