EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell block Comment

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell block Comment

PowerShell block Comment

Introduction to PowerShell comment

For any programmer, commenting on the code is as important as the functionality of the code. Commenting is an important of programming as it helps programmers to understand the code and functionality that is developed by some other programmer. Writing descriptive and insightful PowerShell comments in scripts helps us humans understand the code’s meaning, the effect, and possibly explains edge cases that have arisen in the past. This article will cover in detail about different types of comments, when and where to use which one them effectively in scripts in detail.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

In PowerShell to comment, # is used. Everything that follows # in the same line is considered a comment by the PowerShell interpreter. This is used for single-line commenting, for multi-line commenting, or a comment block <#….#> is used.

Eg:

#example of single ling comment
Write-Host "The above line won't be printed"
<#
the below lines
won't be printed"
#>
Write-Host "above lines are example of multiline comment r script block"

Output:

powershell 1

Commenting out using shortcuts:

To individually comment out each line (#), select one or more lines and press Ctrl + Q, or click Comment (in the Edit section of the Home tab). Highlight a code block and press Ctrl + Shift + Alt + Q, or select Block Message to add a comment block. (PowerShell Studio version 4.1.72 introduces the Block Comment icon.) To delete comments (in either format), select the lines to be deleted and press Ctrl + Delete. Comments can be deleted using Ctrl +shift+ Q or select the block and select uncomment from options window.

Adding Descriptive text for comments:

When you open the script to seek out what the script does, it sure would be nice if there are useful details listed inside like what the script is for like who created it, and when was it created. A block comment in PowerShell is beneficial for adding descriptive text in your scripts. This way, the aim of the script is already given and is additionally an excellent thanks to include warnings or things to observe out for when using the script.

Example:

<# This script will copy items from one site collection to another.
WARNING: Don’t run this in production using system account.
Last update: Feb 14, 2021
Last Updated By: Vignesh Krishnakumar
Last run time: 1 hour 30 mins
Duration: Run every month 15th
Inputs to be supplied: Source site, destination site
Output location: C:\output
#>

powershell 2

Creating comments for documentation purpose:

Sometimes, the script should be run in a way or an order. It will be not be known to a person who has not developed it. In that case, documenting the steps inside a script block will be useful.

Popular Course in this category
Sale
Shell Scripting Training (4 Courses, 1 Project)4 Online Courses | 1 Hands-on Project | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (9,069 ratings)
Course Price

View Course

Related Courses
All in One Data Science Bundle (360+ Courses, 50+ projects)Data Visualization Training (15 Courses, 5+ Projects)

Example:

Input:

<#

This function fetches credentials from an XML file in an encrypted way to connect to the system

In case XML file is not there, please do the below.

1. Open PowerShell as an administrator
2. Run this command – ‘Get-Credential | Export-CliXml .\test.XML’
3. Save the output in the same folder as the script.
4. Once the credentials are received call the main function
5. Once the script is run successfully go and check the logs in the log path
6. In case of any errors copy the id and go and check in CRM

NOTE: This will work only in the same computer
#>

Best Practices for commenting

Never use line number in comments:

When writing code, you would possibly be tempted to feature regard to a line number in your comments. you’ll be right in thinking that adding line number references makes your code that much easier to know. that’s if you’re sure that your code will never be modified. Imagine that a replacement line of code is inserted into your script. This can mean that each one of the road number references in your comments already got shifted. And to stay the road numbering accurate, you’ll need to edit the comments to update them one by one.

Do not add comments at the end of a script:

Generally, a code is read from top to bottom. If the comment you’re adding is vital for the succeeding lines of codes, then it’s only logical to feature the comments before. After all, if you’re adding comments within the script about the script, isn’t it more sensible to possess the comments before the code and not after?

Avoid comment at the end of a line:

Same as the above point, putting comments after the code, albeit it’s on an equivalent line, is not any better than placing the comment below the code. Adding comments at the top of the code can cause editing errors because rather than being focused on modifying the code, you’d even have to mind that the comment moves along because the code changes.

Avoid comments if not needed:

Sometimes the code is too simple and therefore the intent is already too obvious that adding a comment there is simply a waste. Generally during this situation, a comment might be even longer than the code that it refers to. An obvious command with a comment. If you think that the code is self-explanatory, you’ll want to think about skipping adding a comment.

Comment based help for functions and scripts:

Comment-based assistance is written as a series of comments. All the lines within the comment block are interpreted as comments. All the lines during a comment-based help topic must be contiguous. If a comment-based help topic follows a comment that’s not a part of the assistance topic, there must be a minimum of one blank line between the last non-help comment line and therefore the beginning of the comment-based help. Keywords define each section of comment-based help. Each comment-based help keyword is preceded by a dot. The keywords can appear in any order. The keyword names aren’t case-sensitive. For example, Description keyword precedes an outline of a function or script.

Conclusion

Thus, the article explained in detail about commenting or comment block in PowerShell. It also explained the various shortcuts that are available and usage of each comment type. To learn more in detail it is advisable to write sample scripts and practice them.

Recommended Articles

This is a guide to PowerShell block Comment. Here we discuss Introduction, syntax, and How filter function works in Kotlin? with examples. You may also have a look at the following articles to learn more –

Recommended Articles

This is a Guide to PowerShell block Comment. Here we discuss Introduction, syntax, and parameters, examples with code implementation. You may also have a look at the following articles to learn more –

  1. PowerShell where
  2. PowerShell XML
  3. PowerShell Wait
  4. PowerShell uninstall module

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Shell Scripting Course Learn More