How to Add Comments to a PowerShell Script
Adding comments to a PowerShell script explains what the code does, making it easier to understand and maintain later. Windows 11’s PowerShell supports comments that it ignores when running, YYGACOR Login letting you annotate your scripts freely.
The Command
# This is a comment explaining the next line
What It Does
In PowerShell, anything after a `#` on a line is a comment, ignored when the script runs. So you can place explanations above or beside commands to describe their purpose. Comments are purely for humans reading the script and have no effect on execution, making them valuable for clarifying intent in your code.
When You’d Use This
This is good practice in any script you will revisit or share, since comments explain what the code does and why, making it far easier to understand later. Block comments also help during testing by temporarily disabling sections without deleting them. Well-commented scripts save time and confusion when you return to them after weeks or hand them to someone else.
Useful Variations
For a comment spanning multiple lines, PowerShell uses block comments enclosed between `<#` and `#>`, useful for longer explanations or temporarily disabling several lines. You can also add a comment at the end of a line of code, after the command, to annotate that specific line without needing a separate line for it.
If It Doesn’t Work
If a comment seems to break your script, ensure the `#` comes before the text you want ignored, and that block comments are properly closed between `<#` and `#>`. Good comments explain the reasoning rather than just restating the command, since the why is what is hard to reconstruct later. Use block comments to disable sections temporarily during testing rather than deleting them outright.
Good to Know
Good comments explain why code does something rather than merely restating what it does, since the reasoning is what is hard to reconstruct later. Comments also help when returning to a script after time away, and block comments between `<#` and `#>` are handy for temporarily disabling sections during testing without deleting them.
Putting It Together
Once you have run it once or twice, this becomes second nature. As part of working with output and building simple automation, this command is a building block you will reuse constantly. As you combine it with the other scripting basics here, small one-off commands grow into reusable scripts that save real time on repetitive work. Like anything in the terminal, the real value comes from trying it on your own system and adapting the variations above to what you actually need, so it is worth experimenting with in a safe, low-stakes situation before relying on it in a script or during troubleshooting. Keeping a note of the commands you find most useful, along with the variations that fit your workflow, turns scattered one-off tricks into a personal reference you can draw on whenever a similar task comes up again.