PowerShell Comments Multiple lines

A comment is an important part of development through comments we determine what the program is doing. By placing human-readable comments others can also understand and maintain the program. In this article, I will show you how to use PowerShell comments multiple lines as well as single lines.

PowerShell Comments Multiple lines


PowerShell Comments Multiple Lines –

People Generally use single-line comments but what if you have to write a detailed description about the script or code at starting of the program or need to comment out multiple lines in one go then multiple line comments can be used.

To comments, Multiple lines in PowerShell put <# in starting and end with #>.

See below example –

This is how you can comments out multiple lines in PowerShell.
In this script I have commented out below write-host command
$var = “Geeks Tutorials”
Write-Host  $var

This script didn’t produce any output because everything is commented using multiple-line comments. This is how you can use PowerShell to comment out multiple lines.

Multiple line comments are useful when you have to comment on some code piece quickly and execute the program.

PowerShell comments multiple line Example –

<# This program copies file from one location to another
Pass Source and Destination Path.
Copy item command use to copy file.
$SourcefilePath = “C:\sourcepath\test1.txt”
$folderPathDest = “C:\destpath\”
#Copy items
Copy-Item -Path $SourcefilePath -Destination $folderPathDest –PassThru

Below is the PowerShell ISE editor screenshot to show you how multiple line comments look like –

PowerShell comments multiple lines

PowerShell Single Line Comment

This is most popular and easy to implement just use #(hash) to comment out any line but if you have a huge script and you have to comment 10 lines it is always recommended to use multiple lines comments.

Example –

#Write –host “this line is commented”

The above write-host command has been commented using single-line comment.

Benefits of PowerShell comment multiple lines

  • Code maintainability would be easy.
  • User-friendly comments will help other developers to change it later.
  • Easy to Understand.

This is how you can use PowerShell comments multiple lines as well as a single line to comment out code. Commented code improves the program’s maintainability and documentation.