To add comments in PowerShell, start a line with # for a single-line comment, or wrap several lines in <# and #> for a block comment. PowerShell skips both when the script runs:
# This line is a comment
$total = 405 # so is this end-of-line note
<#
This block comment
spans two lines
#>
Below I go from those basics to comment-based help that Get-Help can read, the nesting trap, #Requires and #region, and comments inside JSON and CSV, with the real output from PowerShell 7 and Windows PowerShell 5.1.
Add a single-line comment with #
Everything from # to the end of the line is a comment. It can sit on its own line or after code, which Microsoft calls an end-of-line comment. Putting # in front of a line is also the quickest way to switch that line off:
# Shipping rates in USD per pound
$rates = @{ Standard = 2.25; Express = 4.5 }
$weight = 4 # package weight in pounds
# $weight = 10 # old test value, commented out
"Express cost: $(5.5 + $weight * $rates.Express)"
Output:
Express cost: 23.5
Only the last line printed anything. The rate note, the weight note and the old test value were all ignored. Clear PowerShell naming conventions cut down how many of these notes you need in the first place.
Write a multi-line block comment with <# #>
A block comment starts with <# and ends with #>. It can cover any number of lines, and it can even sit in the middle of a line between two commands:
<#
Daily order report for the Austin warehouse.
Reads the order list, totals it and prints one line.
#>
$orders = 120, 75, 210
$total = ($orders | Measure-Object -Sum).Sum
"Orders: $($orders.Count)"; <# inline note #> "Total: $total USD"
Output:
Orders: 3
Total: 405 USD
The header block is a good place for what the script does and who it’s for. The inline <# inline note #> didn’t stop the second command from running, because PowerShell drops the comment and keeps the rest of the line.
Block comments are handy for switching off a whole if-else statement in PowerShell or a loop while you test, as long as that code doesn’t contain a #> of its own.
Can you nest block comments in PowerShell?
No. The outer block ends at the first #> PowerShell finds, so whatever comes after it runs as code. Here’s what happens when you comment out code that already holds a block comment:
<#
'Foo'
<# 'Bar' #>
'Baz'
#>
'Script finished'
Output:
Baz
Script finished

‘Bar’ and ‘Foo’ stayed hidden, but ‘Baz’ printed. The last #> didn’t cause an error, because a line that starts with # is just another single-line comment.
This bites most often when you wrap a function that has comment-based help. Put # in front of each line instead, which every editor can do for a selection.
When is # not a comment?
Inside single-quoted or double-quoted strings, # and <# #> are plain text. They’re also part of the word when there’s no space in front of them:
'# This is a string, not a comment'
"Total <# still text #> here"
Write-Output Seattle#WA # this part is a comment
try { Foo#Bar } catch { "Failed. $($_.Exception.Message.Split([char]10)[0])" }
Output:
# This is a string, not a comment
Total <# still text #> here
Seattle#WA
Failed. The term 'Foo#Bar' is not recognized as a name of a cmdlet, function, script file, or executable program.
Write-Output got the single argument Seattle#WA, and Foo#Bar was treated as one command name. Only the # after the spaces started a real comment. PowerShell 7 and 5.1 word that last error a little differently, but both refuse to run it.