C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
PowerShell CommentsWhen you want to help others by providing the information about a code, then you must use the comments in that code. Just like other programming or scripting languages, you can give the comments in a PowerShell for the documentation purpose. In PowerShell, there are two types of comments:
Single line commentSingle line comments are those comments in which you can type a hash symbol # at the beginning of each line. Everything to the right of the hash symbol will be ignored. If you write the multiple lines in a script, you had to use the hash # symbol at the starting of each line. Syntax of Single line commentThe following are the two syntaxes for the single-line comment: Syntax1: <Any Command or statement> # <Any comment> Syntax2: # <Any comment> <Any Command or statement> ExamplesExample1: This example displays how to use the comment at the end of a line PS C:\> get-childitem #this command displays the child items of the C: drive Example2: This example displays how to use the comment before the code and at the end of any statement. PS C:\> #This code is used to print the even numbers from 1 to 10 PS C:\> for($i = 1; $i -le 10; $i++) # This loop statement initialize variable from 1 and increment upto 10. >> { >> $x=$i%2 >> if($x -eq 0) # The if condition checks that the value of variable x is equalt to 0, if yes then execute if body >> { >> echo $i # This statement prints the number which is divisibel by 2 >> } >> } Output: 2 4 6 8 10 Multiple line commentWith PowerShell 2.0 or above, multiple line comments or block comments have been introduced. To comment the multiple lines, put the <# symbol at the beginning of the first line and #> symbol at the end of the last line. Syntax of multiple line commentThe following block displays the syntax of multiple line comment: <# Multiple line Comment......... ......... ....................#> Statement-1 Statement-2 Statement-N Example: The following example describes how to use the multiple line comment in code. PS C:\> <# This code is used to print the >> factorial of a given number#> PS C:\> $a=5 PS C:\> $fact=1 PS C:\> for ($i=$a;$i -ge 1;$i--) >> { >> $fact=$fact * $i; >> } Type the following command to display the output of above example: PS C:\> $fact 120
Next TopicWhat is PowerShell cmdlet
|