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












1















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)










share|improve this question























  • Get-Help about_Comment_Based_Help

    – Vivek Kumar Singh
    Jan 2 at 14:21
















1















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)










share|improve this question























  • Get-Help about_Comment_Based_Help

    – Vivek Kumar Singh
    Jan 2 at 14:21














1












1








1








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)










share|improve this question














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)







powershell powershell-ise






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 14:15









WhiskerBiscuitWhiskerBiscuit

1,92863272




1,92863272













  • Get-Help about_Comment_Based_Help

    – Vivek Kumar Singh
    Jan 2 at 14:21



















  • Get-Help about_Comment_Based_Help

    – Vivek Kumar Singh
    Jan 2 at 14:21

















Get-Help about_Comment_Based_Help

– Vivek Kumar Singh
Jan 2 at 14:21





Get-Help about_Comment_Based_Help

– Vivek Kumar Singh
Jan 2 at 14:21












4 Answers
4






active

oldest

votes


















4














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





share|improve this answer
























  • You beat me by 7secs 😉

    – LotPings
    Jan 2 at 14:40











  • @LotPings - wheeeeee! [grin]

    – Lee_Dailey
    Jan 2 at 14:41






  • 1





    Wish I could accept both.

    – WhiskerBiscuit
    Jan 2 at 14:42






  • 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 2 at 14:47





















3














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





share|improve this answer
























  • Holy cow. We've been doing it wrong all these years

    – WhiskerBiscuit
    Jan 2 at 14:46





















1














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






share|improve this answer































    0














    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





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007913%2fis-it-possible-to-use-comments-in-powershell-multiple-line-commands%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      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





      share|improve this answer
























      • You beat me by 7secs 😉

        – LotPings
        Jan 2 at 14:40











      • @LotPings - wheeeeee! [grin]

        – Lee_Dailey
        Jan 2 at 14:41






      • 1





        Wish I could accept both.

        – WhiskerBiscuit
        Jan 2 at 14:42






      • 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 2 at 14:47


















      4














      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





      share|improve this answer
























      • You beat me by 7secs 😉

        – LotPings
        Jan 2 at 14:40











      • @LotPings - wheeeeee! [grin]

        – Lee_Dailey
        Jan 2 at 14:41






      • 1





        Wish I could accept both.

        – WhiskerBiscuit
        Jan 2 at 14:42






      • 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 2 at 14:47
















      4












      4








      4







      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





      share|improve this answer













      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






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 2 at 14:37









      Lee_DaileyLee_Dailey

      2,4861811




      2,4861811













      • You beat me by 7secs 😉

        – LotPings
        Jan 2 at 14:40











      • @LotPings - wheeeeee! [grin]

        – Lee_Dailey
        Jan 2 at 14:41






      • 1





        Wish I could accept both.

        – WhiskerBiscuit
        Jan 2 at 14:42






      • 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 2 at 14:47





















      • You beat me by 7secs 😉

        – LotPings
        Jan 2 at 14:40











      • @LotPings - wheeeeee! [grin]

        – Lee_Dailey
        Jan 2 at 14:41






      • 1





        Wish I could accept both.

        – WhiskerBiscuit
        Jan 2 at 14:42






      • 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 2 at 14:47



















      You beat me by 7secs 😉

      – LotPings
      Jan 2 at 14:40





      You beat me by 7secs 😉

      – LotPings
      Jan 2 at 14:40













      @LotPings - wheeeeee! [grin]

      – Lee_Dailey
      Jan 2 at 14:41





      @LotPings - wheeeeee! [grin]

      – Lee_Dailey
      Jan 2 at 14:41




      1




      1





      Wish I could accept both.

      – WhiskerBiscuit
      Jan 2 at 14:42





      Wish I could accept both.

      – WhiskerBiscuit
      Jan 2 at 14:42




      1




      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 2 at 14:47







      @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 2 at 14:47















      3














      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





      share|improve this answer
























      • Holy cow. We've been doing it wrong all these years

        – WhiskerBiscuit
        Jan 2 at 14:46


















      3














      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





      share|improve this answer
























      • Holy cow. We've been doing it wrong all these years

        – WhiskerBiscuit
        Jan 2 at 14:46
















      3












      3








      3







      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





      share|improve this answer













      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






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 2 at 14:37









      LotPingsLotPings

      19.9k61633




      19.9k61633













      • Holy cow. We've been doing it wrong all these years

        – WhiskerBiscuit
        Jan 2 at 14:46





















      • Holy cow. We've been doing it wrong all these years

        – WhiskerBiscuit
        Jan 2 at 14:46



















      Holy cow. We've been doing it wrong all these years

      – WhiskerBiscuit
      Jan 2 at 14:46







      Holy cow. We've been doing it wrong all these years

      – WhiskerBiscuit
      Jan 2 at 14:46













      1














      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






      share|improve this answer




























        1














        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






        share|improve this answer


























          1












          1








          1







          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 14:20









          elMeroMeroelMeroMero

          1088




          1088























              0














              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





              share|improve this answer




























                0














                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





                share|improve this answer


























                  0












                  0








                  0







                  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





                  share|improve this answer













                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 14:21









                  Ctznkane525Ctznkane525

                  5,94321033




                  5,94321033






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007913%2fis-it-possible-to-use-comments-in-powershell-multiple-line-commands%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      Npm cannot find a required file even through it is in the searched directory