Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 267
WhiskerBiscuit Avatar asked Dec 04 '22 19:12

WhiskerBiscuit


1 Answers

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
like image 178
Lee_Dailey Avatar answered Dec 06 '22 20:12

Lee_Dailey