EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell comment
Secondary Sidebar
PowerShell Tutorial
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • 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 Version Command
    • 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

PowerShell comment

PowerShell comment

Introduction to PowerShell comment

PowerShell comment is one kind of help to give the basic short details of the code. You can add a comment to any section of the PowerShell script, like a comment you can provide for the function or comment you can provide for the conditions, loops, etc. The scripter who is reading the code will easily understand what the code is about. We can also use the comments to provide the help sections. The comment won’t be displayed when the script is executed; it is only for the user information.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,171 ratings)

Types of PowerShell comment

There are two types of PowerShell comments we can use:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • Single Line Comment: This is also called the Inline comment, and it can comment out only one line.
  • Multi-Line comment: This is also called the block comment, and it is applied to the few line of codes or the whole code.

Syntax:

Single line comment or Inline comment can be described with Hash (#) syntax while the multi-line comments or block comments can be described with the tags and Hash like <#..#>

Examples

Given below are the examples of PowerShell comment:

Example #1

Single line Comment to describe the code.

Code:

Rename-Item C:\Temp\azgroup.json -NewName azuregroup.json  # This command will rename the item

Example #2

Single Comment to describe the function.

Code:

function GetData{
#Function will get the data from azure blob
}

Example #3

Comment to disable the specific code.

Code:

# Copy-Item C:\Temp\NewUsers.csv -Destination \\test1-win2k12\c$\temp

When you comment on any code, you may notice that the color of the line changes. It depends on the editor and the theme you applied for that editor. If you have the block that describes the multiple lines together like ” loop, ” ” function, ” ” for loop, ” etc., and if you apply comment for a single line, that might generate an error.

Code:

while($i -gt 10){
Write-Output $i
$i++
}

If you disable the first line using a comment in the above example, it will generate an error.

Code:

# while($i -gt 10){
Write-Output $i
$i++
}

Output:

it will generate an error

The error says the additional ‘}’ is not expected because we have already disabled the ‘{‘. You can use the single-line comment to disable the multiple lines of code.

Code:

# while($i -gt 10){
#  Write-Output $i
#  $i++
#}

But this method is not recommended; instead, we can use the multi-line comment to disable them, and it is shown in the subsequent examples.

Example #4

Multi-Line comment to describe script.

Code:

<#
This program is to test the server connectivity.
Once connectivity establishes it checks the WINRM connection on the server.
The third step is to push the Application agent on the servers
In the final step, server will be rebooted
#>

In the above example, we have created a multi-line comment block using PowerShell, and it won’t be executed. It is used when you want to put the specific details about parameters, for example, date format, or the user should note before executing the script like the output path of the logs.

Example #5

Multi-line comment to disable the function or part of the code.

As shown in example ‘c’, we can disable the entire while loop using single-line comments, but it would be much easier to use the multiline comments.

Code:

<#while($i -gt 10){
Write-Output $i
$i++
}#>

It is much easier than the single-line comment.

PowerShell comments Based Help

This is also a type of multi-line comment. PowerShell comments based help work on Keywords. Each Keyword describes the specific topics for their intended use, and they can be used inside the multi-line comment section.

These keywords can appear in any order, and they are not case-sensitive, and they start with a dot (.). Mentioning all keywords are not necessary. You can add a few of them as per your requirement.

  • .SYNOPSIS: A brief description of the function or the script. This keyword is used only once.
  • .DESCRIPTION: A detailed description of the script. This keyword is used only once.
  • .EXAMPLE: A sample example for the script along with input and output. We can add multiple examples.
  • .PARAMETER <Parameter-Name>: Description of the parameter. This keyword can be used for each parameter in the script. It can be in any order, but the order in which they are mentioned in the Param() block will appear in the help section.
  • .INPUTS: Description of the input objects or the types of objects that can be piped to the script or function.
  • .OUTPUTS: Description of the returned object or the types of the objects that the cmdlet returns.

The above are the basic keywords that we generally need to describe the comment-based help section of the function. Apart from that, there are other keywords that you can also specify, Like NOTES, LINK, COMPONENT, ROLE, FUNCTIONALITY, etc.

Example:

Code:

function TestRemotePort{
param(
[String]$ComputerName,
[Int]$Port
)
<#
.SYNOPSIS
Function to test remote ports.
.DESCRIPTION
This script checks port on the remote computers
.EXAMPLE
TestRemotePort -ComputerName ServerName -Port 8080
.PARAMETER ComputerName
Remote Server Name
.PARAMETER Port
Remote port number
.INPUTS
ComputerNames can be passed and a port can be passed
.OUTPUTS
True or False
#>
Test-NetConnection -ComputerName $ComputerName -Port $Port
}

Output:

PowerShell comment 16

PowerShell comment 18

You can also place the comment-based help at the beginning of the script.

Code:

<#
.DESCRIPTION
This script will test the connection with the remote server
#>
param(
[Parameter(Mandatory=$true)] [String]$Server
)
Test-Connection -ComputerName $Server -Count 2 -quiet

Output:

beginning of the script

And the comment-based help can be placed at the end of the script.

Code:

function TestNetConnection{
Test-Netconnection -ComputerName $computername -Port $port
}
<#
.DESCRIPTION
The above function checks the port on the remote computer.
#>

Recommended Articles

This is a guide to the PowerShell comment. Here we discuss the introduction, types, examples and PowerShell comments based help. You may also have a look at the following articles to learn more –

  1. PowerShell Escape Character
  2. PowerShell not like
  3. PowerShell Continue
  4. Windows PowerShell ISE
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
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

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*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.

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

*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.

Let’s Get Started

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