Is it possible to use comments in Powershell multiple line commands?
Asked Answered
L

4

9

Debugging and testing multiline commands in Powershell ISE has been bugging me for years. I like having multiple line commands because they are easy to read, but they make things harder to debug. As an example, I'm using the following command to get folders older than $days (which by the way works).

$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 `
    | Where CreationTime -gt (Get-Date).AddDays(-1 * $days) `
    | Sort-Object -Property LastWriteTime

I'd like to change AddDays to AddMinutes to test different result sets but I want to leave the original line in so I can easily switch back and forth. Below I copied the line I want to keep and commented it out, and on the new line changed AddDays to AddMinutes Adding a # breaks the multiline feature. Is there an easy way around this I don't have to cut my copied line and move it "out" of the command? Or is there a way to split/unsplit a command into and out of multiple lines?

$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 `
#    | Where CreationTime -gt (Get-Date).AddDays(-1 * $days) `
    | Where CreationTime -gt (Get-Date).AddMinutes(-1 * $days) `
    | Sort-Object -Property LastWriteTime

(above does not work due to commented out line)

Lacker answered 2/1, 2019 at 14:15 Comment(1)
Get-Help about_Comment_Based_Help – Leukemia
P
5

your problem is the [icky, nasty] backticks. [grin] powershell knows there is more coming after a pipe ... so there is no need to add a backtick if you put the pipe at the end of the segment that is being piped. like this ...

$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 |
    # Where CreationTime -gt (Get-Date).AddDays(-1 * $days) |
    Where CreationTime -gt (Get-Date).AddMinutes(-1 * $days) |
    Sort-Object -Property LastWriteTime
Portfire answered 2/1, 2019 at 14:37 Comment(7)
You beat me by 7secs πŸ˜‰ – Elroy
@LotPings - wheeeeee! [grin] – Portfire
@Lacker - that is a real trial at times ... "multiple good responses, only one can be accepted" can be partially dealt with by up-voting them both, tho. [grin] – Portfire
This solution is very specific to pipes, but not any other multi line scenario. <# comment #> is a correct solution – Willy
@Willy - yep! the question seemed specific, so my answer was specific. the general answer is to use natural line continuations [commas, open brace/paren, dot notation, multiline comments, etc ...] instead of backticks. [grin] – Portfire
This does not work for cases where you split one command with multiple named parameters over multiple lines. And I agree that these backticks are not pretty (and would wish to use something else), however they are what we have and need to use. – Escalade
This shouldn't be the accepted answer. https://mcmap.net/q/501957/-is-it-possible-to-use-comments-in-powershell-multiple-line-commands should be. – Miun
S
10

Use the multi-line comment syntax (<##>) instead of single-line comment syntax (#):

Invoke-Command              <# Multi-line comment #1. #> `
    -Parameter1="$ValueOne" <# Multi-line comment #2. #> `
    -Parameter2="$ValueTwo"  # Single-line comment. This is fine because it's at the end of the line.

This should allow you to comment text within a multi-line command. However, this works only if you are using PowerShell 2.0.

Stupa answered 2/1, 2019 at 14:20 Comment(1)
Note that the backtick still needs to come after the <##>. Submitted as stackoverflow.com/suggested-edits/5625205. – Miun
P
5

your problem is the [icky, nasty] backticks. [grin] powershell knows there is more coming after a pipe ... so there is no need to add a backtick if you put the pipe at the end of the segment that is being piped. like this ...

$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 |
    # Where CreationTime -gt (Get-Date).AddDays(-1 * $days) |
    Where CreationTime -gt (Get-Date).AddMinutes(-1 * $days) |
    Sort-Object -Property LastWriteTime
Portfire answered 2/1, 2019 at 14:37 Comment(7)
You beat me by 7secs πŸ˜‰ – Elroy
@LotPings - wheeeeee! [grin] – Portfire
@Lacker - that is a real trial at times ... "multiple good responses, only one can be accepted" can be partially dealt with by up-voting them both, tho. [grin] – Portfire
This solution is very specific to pipes, but not any other multi line scenario. <# comment #> is a correct solution – Willy
@Willy - yep! the question seemed specific, so my answer was specific. the general answer is to use natural line continuations [commas, open brace/paren, dot notation, multiline comments, etc ...] instead of backticks. [grin] – Portfire
This does not work for cases where you split one command with multiple named parameters over multiple lines. And I agree that these backticks are not pretty (and would wish to use something else), however they are what we have and need to use. – Escalade
This shouldn't be the accepted answer. https://mcmap.net/q/501957/-is-it-possible-to-use-comments-in-powershell-multiple-line-commands should be. – Miun
E
5

As powershell expects a continuation after a | or a ,
as the last char in a line you don't need the backtick and
you could format differently, then the single line comment in a longer pipe still works:

$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 |
#   Where CreationTime -gt (Get-Date).AddDays(-1 * $days) |
    Where CreationTime -gt (Get-Date).AddMinutes(-1 * $minutes) |
    Sort-Object -Property LastWriteTime
Elroy answered 2/1, 2019 at 14:37 Comment(1)
Holy cow. We've been doing it wrong all these years – Lacker
C
4

Try this, which can be included as a multiline comment example

$dirs = Get-ChildItem $targetDir -Directory -exclude *.ps1 `
<#    | Where CreationTime -gt (Get-Date).AddDays(-1 * $days) #> `
    | Where CreationTime -gt (Get-Date).AddMinutes(-1 * $days) `
    <# also comments explaining what you are doing can be included #> `
    | Sort-Object -Property LastWriteTime
Cuirass answered 2/1, 2019 at 14:21 Comment(1)
The important thing here is the backtick after the comment. This seems to allow for a line break after that backtick. At least for powershell 7.4.0. – Selfdenial

© 2022 - 2024 β€” McMap. All rights reserved.