6

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)

WhiskerBiscuit
  • 4,795
  • 8
  • 62
  • 100

4 Answers4

6

Use the multiline comment syntax instead of #.

<# comment #> 

This should allow you to comment text within a multi-line command.

However, this works only if you are using Powershell 2.0

elMeroMero
  • 752
  • 6
  • 18
6

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
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
  • 1
    You beat me by 7secs –  Jan 02 '19 at 14:40
  • @LotPings - wheeeeee! [*grin*] – Lee_Dailey Jan 02 '19 at 14:41
  • 1
    @WhiskerBiscuit - 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*] – Lee_Dailey Jan 02 '19 at 14:47
  • This solution is very specific to pipes, but not any other multi line scenario. `<# comment #>` is a correct solution – mr.buttons Nov 14 '19 at 18:08
  • @mr.buttons - 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*] – Lee_Dailey Nov 14 '19 at 19:19
6

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
3

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) `
    | Sort-Object -Property LastWriteTime
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40