11---
2- description : Describes how to use comments and lists special use cases
2+ description : Describes how to use comments and lists special use cases.
33Locale : en-US
44ms.date : 01/10/2025
5- online version :
5+ online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_comments?view=powershell-5.1&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about_Comments
88---
@@ -17,26 +17,33 @@ Describes how to use PowerShell comments and lists special use cases.
1717You can write comments to annotate or structure your PowerShell code to help
1818with readability. When your code is run, comment text is ignored by PowerShell.
1919
20- Comments are typically used to provide important context to readers of the code
21- that may not be apparent from the code alone. This may include explanations on
22- why a particular approach was chosen, edge cases to be aware of or links to
23- supporting reference material.
20+ Comments provide important context to readers of the code. You should use
21+ comments for the following purposes:
2422
25- Some comments have special meaning in PowerShell. See [ Special comments] [ 01 ] .
23+ - Explain complex code in simpler terms
24+ - Explain why a particular approach was chosen
25+ - Document edge cases to be aware
26+ - Provide links to supporting reference material
27+
28+ Some comments have special meaning in PowerShell. See [ Special comments] [ 04 ] .
2629
2730## PowerShell comment styles
2831
29- Comments are designated by the ` # ` or ` <# #> ` characters.
32+ PowerShell supports two comment styles:
33+
34+ - ** Single-line comments** begin with hash character (` # ` ) and end with a
35+ newline. The ` # ` can be preceded by text that's not a part of the comment,
36+ including whitespace. Single-line comments placed on the same line as
37+ uncommented source code are known as end-of-line comments.
38+ - ** Block comments** begin with ` <# ` and end with ` #> ` . A block comment can
39+ span any number of lines, and can be included before, after or in the middle
40+ of uncommented source code. All text within the block is treated as part of
41+ the same comment, including whitespace.
3042
31- - ** Single-line comments** begin with ` # ` and end with a newline. The ` # ` may
32- be preceded by text not a part of the comment, including whitespace.
33- Single-line comments placed on the same line as uncommented source code are
34- end-of-line comments.
35- - ** Block comments** begin and end with a matching pair of ` <# ` and ` #> `
36- characters. All text within the block is treated as part of the same comment,
37- including whitespace. A block comment can span any number of lines,
38- and can be included before, after or in the middle of uncommented source
39- code.
43+ > [ !IMPORTANT]
44+ > You can include single-line comments within a block comment. However, you
45+ > can't nest block comments. If you attempt to nest block comments, the outer
46+ > block comment ends at the first ` #> ` encountered.
4047
4148## Examples
4249
@@ -94,12 +101,15 @@ $var.Where(
94101
95102## Special comments
96103
104+ PowerShell includes several comment keywords to support specific uses.
105+
97106### Comment-based help
98107
99108You can write comment-based help content for functions and scripts using either
100- single-line or block comments. Comment-based help is used by the [ Get-Help] [ 02 ]
101- cmdlet to output information such as the description and example usage of a
102- function or script.
109+ single-line or block comments. Users can use the [ Get-Help] [ 14 ] cmdlet to view
110+ comment-based help for a function or script. PowerShell defines 15 comment
111+ keywords that can be used to provide information such as the description and
112+ example usage.
103113
104114``` powershell
105115<#
@@ -117,16 +127,15 @@ function Get-Function { }
117127
118128For more information, see:
119129
120- - [ about_Comment_Based_Help] [ 03 ] .
121- - [ Writing Comment-Based Help Topics] [ 04 ]
130+ - [ about_Comment_Based_Help] [ 05 ] .
131+ - [ Writing Comment-Based Help Topics] [ 03 ]
122132
123133### ` #Requires `
124134
125- The ` #Requires ` statement prevents a script from running unless the PowerShell
126- version, modules (and version), and edition prerequisites are met.
127-
128- ` #Requires ` can appear on any line in a script, but is processed in the same
129- manner regardless of position.
135+ The ` #Requires ` statement prevents a script from running unless the current
136+ PowerShell sessions meets the specified prerequisites. ` #Requires ` can appear
137+ on any line in a script, but is processed in the same manner regardless of
138+ position.
130139
131140``` powershell
132141#Requires -Modules AzureRM.Netcore
@@ -138,7 +147,7 @@ param (
138147)
139148```
140149
141- For more information, see [ about_Requires] [ 05 ] .
150+ For more information, see [ about_Requires] [ 09 ] .
142151
143152### Signature block
144153
@@ -153,12 +162,25 @@ before the script is executed.
153162# SIG # End signature block
154163```
155164
156- For more information, see [ about_signing] [ 06 ] .
165+ For more information, see [ about_signing] [ 10 ] .
166+
167+ ### Code editor region markers
168+
169+ Some code editors support region markers that allow you to collapse and expand
170+ sections of code. For PowerShell, the region markers are comments that begin
171+ with ` #region ` and end with ` #endregion ` . The region markers must be at the
172+ beginning of a line. The region markers are supported in the PowerShell ISE and
173+ in Visual Studio Code with the PowerShell extension. The region markers aren't
174+ a part of the PowerShell language. PowerShell interprets them as a regular
175+ comments.
176+
177+ For more information, see the _ Folding_ section of the
178+ [ Basic editing in Visual Studio Code] [ 11 ] documentation.
157179
158180## Comments in string tokens
159181
160- ` # ` and ` <# #> ` do not have special meaning within an [ expandable] [ 08 ] or
161- [ verbatim] [ 09 ] string. PowerShell interprets the characters literally.
182+ ` # ` and ` <# #> ` don't have special meaning within an [ expandable] [ 06 ] or
183+ [ verbatim] [ 07 ] string. PowerShell interprets the characters literally.
162184
163185``` powershell
164186PS> '# This is not interpreted as a comment.'
@@ -174,7 +196,7 @@ feature.
174196
175197### Regular expression comments
176198
177- Regular expressions (regex) in PowerShell use the [ .NET regex] [ 10 ] engine,
199+ Regular expressions (regex) in PowerShell use the [ .NET regex] [ 02 ] engine,
178200which supports two comment styles:
179201
180202- Inline comment (` (?#) ` )
198220
199221> [ !NOTE]
200222> An end-of-line regex comment requires either the ` (?x) ` construct or the
201- > [ ` IgnorePatternWhitespace ` ] [ 11 ] option.
223+ > [ ` IgnorePatternWhitespace ` ] [ 24 ] option.
202224
203225For more information, see:
204226
205- - [ about_Regular_Expressions] [ 12 ]
206- - [ Miscellaneous Constructs in Regular Expressions] [ 13 ]
227+ - [ about_Regular_Expressions] [ 08 ]
228+ - [ Miscellaneous Constructs in Regular Expressions] [ 01 ]
207229
208230### JSON comments
209231
210- Beginning in PowerShell 6.0, the [ ConvertFrom-Json] [ 14 ] cmdlet supports the
232+ Beginning in PowerShell 6.0, the [ ConvertFrom-Json] [ 16 ] cmdlet supports the
211233following JSON comment styles:
212234
213235- Single-line comment (` // ` )
214236- Block comment (` /* */ ` )
215237
216238> [ !NOTE]
217- > The [ Invoke-RestMethod] [ 15 ] cmdlet automatically deserializes received JSON
239+ > The [ Invoke-RestMethod] [ 22 ] cmdlet automatically deserializes received JSON
218240> data. In PowerShell 6.0 onwards, comments are permitted in the JSON data.
219241
220242For example:
221243
222244``` powershell
223- '{
224- "Foo": "Bar" // This is a single-line comment
245+ '{
246+ "Foo": "Bar" // This is a single-line comment
225247}' | ConvertFrom-Json
226248```
227249
@@ -232,20 +254,42 @@ Bar
232254```
233255
234256> [ !WARNING]
235- > Beginning in PowerShell 7.4, the [ Test-Json] [ 16 ] cmdlet no longer supports
257+ > Beginning in PowerShell 7.4, the [ Test-Json] [ 23 ] cmdlet no longer supports
236258> JSON with comments. An error is returned if the JSON includes comments. In
237259> supported versions prior to 7.4, ` Test-Json ` successfully parses JSON with
238- > comments.
260+ > comments. In PowerShell 7.5, ` Test-Json ` includes an option to ignore
261+ > JSON comments.
239262
240263### CSV comments
241264
242- In Windows PowerShell 5.1, the default [ Export-Csv] [ 20 ] and [ ConvertTo-Csv] [ 21 ]
265+ [ Import-Csv] [ 21 ] and [ ConvertFrom-Csv] [ 15 ] support the W3C Extended Log format.
266+ Lines starting with the hash character (` # ` ) are treated as comments and
267+ ignored unless the comment starts with ` #Fields: ` and contains delimited list
268+ of column names. In that case, the cmdlet uses those column names. This is the
269+ standard format for Windows IIS and other web server logs. For more
270+ information, see [ Extended Log File Format] [ 13 ] .
271+
272+ ``` powershell
273+ @'
274+ # This is a CSV comment
275+ Col1,Col2
276+ Val1,Val2
277+ '@ | ConvertFrom-Csv
278+ ```
279+
280+ ``` Output
281+ Col1 Col2
282+ ---- ----
283+ Val1 Val2
284+ ```
285+
286+ In Windows PowerShell 5.1, the default [ Export-Csv] [ 20 ] and [ ConvertTo-Csv] [ 19 ]
243287behavior is to include type information in the form of a ` #TYPE ` comment.
244288Beginning in PowerShell 6.0, the default is not to include the comment, but
245289this can be overridden with the ** IncludeTypeInformation** parameter.
246290
247291``` powershell
248- [pscustomobject] @{ Foo = 'Bar' } | ConvertTo-Csv
292+ [pscustomobject] @{ Foo = 'Bar' } | ConvertTo-Csv -IncludeTypeInformation
249293```
250294
251295``` Output
@@ -262,39 +306,40 @@ deserialized object(s).
262306class Test { $Foo = 'Bar' }
263307$test = [Test]::new()
264308
265- $var = $test | ConvertTo-Csv | ConvertFrom-Csv
309+ $var = $test | ConvertTo-Csv -IncludeTypeInformation | ConvertFrom-Csv
266310$var.pstypenames
267311```
268312
269313``` Output
314+ Test
270315CSV:Test
271316```
272317
273318### ` ConvertFrom-StringData ` comments
274319
275- Within its string data, the [ ConvertFrom-StringData] [ 22 ] cmdlet treats lines
320+ Within its string data, the [ ConvertFrom-StringData] [ 17 ] cmdlet treats lines
276321beginning with ` # ` as comments. For more information, see:
277322
278- - [ Example 3: Convert a here-string containing a comment] [ 23 ]
323+ - [ Example 3: Convert a here-string containing a comment] [ 18 ]
279324
280325## Notes
281326
282- - Block comments cannot be nested. In the following example, ` Baz ` is not a
327+ - Block comments can't be nested. In the following example, ` Baz ` is not a
283328 part of the comment.
284329
285330 ``` powershell
286331 <#
287- Foo
288- <# Bar #>
289- Baz
332+ ' Foo'
333+ <# ' Bar' #>
334+ ' Baz'
290335 #>
291336 ```
292337
293338- ` <# #> ` has no special meaning within a single-line comment. ` # ` has no
294339 special meaning within a block comment.
295340- To be treated as a comment, the comment character must not be a part
296341 of a non-comment token. In the following example, ` #Bar ` and ` <#Bar#> ` are
297- a part of the same ` Foo... ` token and are therefore not treated as comments.
342+ a part of the ` Foo... ` token. Therefore, they aren't treated as comments.
298343
299344 ``` powershell
300345 PS> Foo#Bar
@@ -305,22 +350,27 @@ beginning with `#` as comments. For more information, see:
305350 ```
306351
307352<!-- link references -->
308- [ 01 ] : #special-comments
309- [ 02 ] : xref:Microsoft.PowerShell.Core.Get-Help
310- [ 03 ] : about_Comment_Based_Help.md
311- [ 04 ] : /powershell/scripting/developer/help/writing-comment-based-help-topics
312- [ 05 ] : about_Requires.md
313- [ 06 ] : about_signing.md
314- [ 08 ] : about_quoting_rules#double-quoted-strings
315- [ 09 ] : about_quoting_rules#single-quoted-strings
316- [ 10 ] : /dotnet/standard/base-types/regular-expressions
317- [ 11 ] : /dotnet/api/system.text.regularexpressions.regexoptions#system-text-regularexpressions-regexoptions-ignorepatternwhitespace
318- [ 12 ] : about_Regular_Expressions.md
319- [ 13 ] : /dotnet/standard/base-types/miscellaneous-constructs-in-regular-expressions
320- [ 14 ] : xref:Microsoft.PowerShell.Utility.ConvertFrom-Json
321- [ 15 ] : xref:Microsoft.PowerShell.Utility.Invoke-RestMethod
322- [ 16 ] : xref:Microsoft.PowerShell.Utility.Test-Json
353+ [ 01 ] : /dotnet/standard/base-types/miscellaneous-constructs-in-regular-expressions
354+ [ 02 ] : /dotnet/standard/base-types/regular-expressions
355+ [ 03 ] : /powershell/scripting/developer/help/writing-comment-based-help-topics
356+ [ 04 ] : #special-comments
357+ [ 05 ] : about_Comment_Based_Help.md
358+ [ 06 ] : about_quoting_rules#double-quoted-strings
359+ [ 07 ] : about_quoting_rules#single-quoted-strings
360+ [ 08 ] : about_Regular_Expressions.md
361+ [ 09 ] : about_Requires.md
362+ [ 10 ] : about_signing.md
363+ [ 11 ] : https://code.visualstudio.com/docs/editor/codebasics#_folding
364+ [ 12 ] : https://wikipedia.org/wiki/Shebang_(Unix)
365+ [ 13 ] : https://www.w3.org/TR/WD-logfile.html
366+ [ 14 ] : xref:Microsoft.PowerShell.Core.Get-Help
367+ [ 15 ] : xref:Microsoft.PowerShell.Utility.ConvertFrom-Csv
368+ [ 16 ] : xref:Microsoft.PowerShell.Utility.ConvertFrom-Json
369+ [ 17 ] : xref:Microsoft.PowerShell.Utility.ConvertFrom-StringData
370+ [ 18 ] : xref:Microsoft.PowerShell.Utility.ConvertFrom-StringData#example-3-convert-a-here-string-containing-a-comment
371+ [ 19 ] : xref:Microsoft.PowerShell.Utility.ConvertTo-Csv
323372[ 20 ] : xref:Microsoft.PowerShell.Utility.Export-Csv
324- [ 21 ] : xref:Microsoft.PowerShell.Utility.ConvertTo-Csv
325- [ 22 ] : xref:Microsoft.PowerShell.Utility.ConvertFrom-StringData
326- [ 23 ] : xref:Microsoft.PowerShell.Utility.ConvertFrom-StringData#example-3-convert-a-here-string-containing-a-comment
373+ [ 21 ] : xref:Microsoft.PowerShell.Utility.Import-Csv
374+ [ 22 ] : xref:Microsoft.PowerShell.Utility.Invoke-RestMethod
375+ [ 23 ] : xref:Microsoft.PowerShell.Utility.Test-Json
376+ [ 24 ] : xref:System.Text.RegularExpressions.RegexOptions#system-text-regularexpressions-regexoptions-ignorepatternwhitespace
0 commit comments