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)
powershell powershell-ise
add a comment |
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
Get-Help about_Comment_Based_Help
– Vivek Kumar Singh
Jan 2 at 14:21
add a comment |
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
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
powershell powershell-ise
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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
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
add a comment |
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
Holy cow. We've been doing it wrong all these years
– WhiskerBiscuit
Jan 2 at 14:46
add a comment |
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
Holy cow. We've been doing it wrong all these years
– WhiskerBiscuit
Jan 2 at 14:46
add a comment |
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
Holy cow. We've been doing it wrong all these years
– WhiskerBiscuit
Jan 2 at 14:46
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 2 at 14:20


elMeroMeroelMeroMero
1088
1088
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 2 at 14:21


Ctznkane525Ctznkane525
5,94321033
5,94321033
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Get-Help about_Comment_Based_Help
– Vivek Kumar Singh
Jan 2 at 14:21