Skip to content

Commit d7352d4

Browse files
Publish to live (#11659)
* Fix typos and add links for PSConsoleHostReadline (#11657) * Add new about_Comments document (#11658) * Add new about_Comments document * Editorial changes and added #region info * fix broken links --------- Co-authored-by: Sean Wheeler <[email protected]> --------- Co-authored-by: surfingoldelephant <[email protected]>
1 parent 07b76b2 commit d7352d4

File tree

9 files changed

+1226
-62
lines changed

9 files changed

+1226
-62
lines changed
Lines changed: 376 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,376 @@
1+
---
2+
description: Describes how to use comments and lists special use cases.
3+
Locale: en-US
4+
ms.date: 01/10/2025
5+
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_comments?view=powershell-5.1&WT.mc_id=ps-gethelp
6+
schema: 2.0.0
7+
title: about_Comments
8+
---
9+
# about_Comments
10+
11+
## Short description
12+
13+
Describes how to use PowerShell comments and lists special use cases.
14+
15+
## Long description
16+
17+
You can write comments to annotate or structure your PowerShell code to help
18+
with readability. When your code is run, comment text is ignored by PowerShell.
19+
20+
Comments provide important context to readers of the code. You should use
21+
comments for the following purposes:
22+
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].
29+
30+
## PowerShell comment styles
31+
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.
42+
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.
47+
48+
## Examples
49+
50+
### Example 1: Single-line comment
51+
52+
```powershell
53+
# This is a single-line comment.
54+
# This is also a single-line comment.
55+
```
56+
57+
### Example 2: Block comment
58+
59+
```powershell
60+
<#
61+
This is a block comment.
62+
Text within the block is a part of the same comment.
63+
Whitespace is unimportant in a block comment.
64+
#>
65+
```
66+
67+
### Example 3: End-of-line comment
68+
69+
```powershell
70+
$var = 4 # This is an end-of-line comment
71+
```
72+
73+
### Example 4: Inline block comment
74+
75+
```powershell
76+
'Foo'; <# This is an inline block comment #> 'Bar'
77+
```
78+
79+
### Example 5: Full example
80+
81+
```powershell
82+
<#
83+
.DESCRIPTION
84+
Demonstrates PowerShell's different comment styles.
85+
#>
86+
param (
87+
[string] $Param1, # End-of-line comment
88+
<# Inline block comment #> $Param2
89+
)
90+
91+
$var = 1, <# Inline block comment #> 2, 2
92+
93+
# Single-line comment.
94+
# Another single-line comment.
95+
$var.Where(
96+
<# Arg1 note #> { $_ -eq 2 },
97+
<# Arg2 note #> 'First',
98+
<# Arg3 note #> 1
99+
)
100+
```
101+
102+
## Special comments
103+
104+
PowerShell includes several comment keywords to support specific uses.
105+
106+
### Comment-based help
107+
108+
You can write comment-based help content for functions and scripts using either
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.
113+
114+
```powershell
115+
<#
116+
.DESCRIPTION
117+
Comment-based help using a block comment.
118+
#>
119+
function Get-Function { }
120+
```
121+
122+
```powershell
123+
# .DESCRIPTION
124+
# Comment-based help using multiple single-line comments.
125+
function Get-Function { }
126+
```
127+
128+
For more information, see:
129+
130+
- [about_Comment_Based_Help][05].
131+
- [Writing Comment-Based Help Topics][03]
132+
133+
### `#Requires`
134+
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.
139+
140+
```powershell
141+
#Requires -Modules AzureRM.Netcore
142+
#Requires -Version 6.0
143+
144+
param (
145+
[Parameter(Mandatory)]
146+
[string[]] $Path
147+
)
148+
```
149+
150+
For more information, see [about_Requires][09].
151+
152+
### Signature block
153+
154+
Scripts can be signed so that they comply with PowerShell execution policies.
155+
Once signed, a signature block is added to the end of a script. This block
156+
takes the form of multiple single-line comments, which are read by PowerShell
157+
before the script is executed.
158+
159+
```powershell
160+
# SIG # Begin signature block
161+
# ...
162+
# SIG # End signature block
163+
```
164+
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.
179+
180+
## Comments in string tokens
181+
182+
`#` and `<# #>` don't have special meaning within an [expandable][06] or
183+
[verbatim][07] string. PowerShell interprets the characters literally.
184+
185+
```powershell
186+
PS> '# This is not interpreted as a comment.'
187+
# This is not interpreted as a comment.
188+
189+
PS> "This is <# also not interpreted #> as a comment."
190+
This is <# also not interpreted #> as a comment.
191+
```
192+
193+
However, certain PowerShell features are designed to work with strings that
194+
contain comments. Interpretation of the comment is dependant on the specific
195+
feature.
196+
197+
### Regular expression comments
198+
199+
Regular expressions (regex) in PowerShell use the [.NET regex][02] engine,
200+
which supports two comment styles:
201+
202+
- Inline comment (`(?#)`)
203+
- End-of-line comment (`#`)
204+
205+
Regex comments are supported by all regex-based features in PowerShell. For
206+
example:
207+
208+
```powershell
209+
PS> 'book' -match '(?# This is an inline comment)oo'
210+
True
211+
212+
PS> 'book' -match '(?x)oo# This is an end-of-line comment'
213+
True
214+
215+
PS> $regex = 'oo # This is an end-of-line comment'
216+
PS> 'book' -split $regex, 0, 'IgnorePatternWhitespace'
217+
b
218+
k
219+
```
220+
221+
> [!NOTE]
222+
> An end-of-line regex comment requires either the `(?x)` construct or the
223+
> [`IgnorePatternWhitespace`][24] option.
224+
225+
For more information, see:
226+
227+
- [about_Regular_Expressions][08]
228+
- [Miscellaneous Constructs in Regular Expressions][01]
229+
230+
### JSON comments
231+
232+
Beginning in PowerShell 6.0, the [ConvertFrom-Json][16] cmdlet supports the
233+
following JSON comment styles:
234+
235+
- Single-line comment (`//`)
236+
- Block comment (`/* */`)
237+
238+
> [!NOTE]
239+
> The [Invoke-RestMethod][22] cmdlet automatically deserializes received JSON
240+
> data. In PowerShell 6.0 onwards, comments are permitted in the JSON data.
241+
242+
For example:
243+
244+
```powershell
245+
'{
246+
"Foo": "Bar" // This is a single-line comment
247+
}' | ConvertFrom-Json
248+
```
249+
250+
```Output
251+
Foo
252+
---
253+
Bar
254+
```
255+
256+
> [!WARNING]
257+
> Beginning in PowerShell 7.4, the [Test-Json][23] cmdlet no longer supports
258+
> JSON with comments. An error is returned if the JSON includes comments. In
259+
> supported versions prior to 7.4, `Test-Json` successfully parses JSON with
260+
> comments. In PowerShell 7.5, `Test-Json` includes an option to ignore
261+
> JSON comments.
262+
263+
### CSV comments
264+
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]
287+
behavior is to include type information in the form of a `#TYPE` comment.
288+
Beginning in PowerShell 6.0, the default is not to include the comment, but
289+
this can be overridden with the **IncludeTypeInformation** parameter.
290+
291+
```powershell
292+
[pscustomobject] @{ Foo = 'Bar' } | ConvertTo-Csv -IncludeTypeInformation
293+
```
294+
295+
```Output
296+
#TYPE System.Management.Automation.PSCustomObject
297+
"Foo"
298+
"Bar"
299+
```
300+
301+
When a `#TYPE` comment is included in CSV data, `Import-Csv` and
302+
`ConvertFrom-Csv` use this information to set the `pstypenames` property of the
303+
deserialized object(s).
304+
305+
```powershell
306+
class Test { $Foo = 'Bar' }
307+
$test = [Test]::new()
308+
309+
$var = $test | ConvertTo-Csv -IncludeTypeInformation | ConvertFrom-Csv
310+
$var.pstypenames
311+
```
312+
313+
```Output
314+
Test
315+
CSV:Test
316+
```
317+
318+
### `ConvertFrom-StringData` comments
319+
320+
Within its string data, the [ConvertFrom-StringData][17] cmdlet treats lines
321+
beginning with `#` as comments. For more information, see:
322+
323+
- [Example 3: Convert a here-string containing a comment][18]
324+
325+
## Notes
326+
327+
- Block comments can't be nested. In the following example, `Baz` is not a
328+
part of the comment.
329+
330+
```powershell
331+
<#
332+
'Foo'
333+
<# 'Bar' #>
334+
'Baz'
335+
#>
336+
```
337+
338+
- `<# #>` has no special meaning within a single-line comment. `#` has no
339+
special meaning within a block comment.
340+
- To be treated as a comment, the comment character must not be a part
341+
of a non-comment token. In the following example, `#Bar` and `<#Bar#>` are
342+
a part of the `Foo...` token. Therefore, they aren't treated as comments.
343+
344+
```powershell
345+
PS> Foo#Bar
346+
Foo#Bar: The term 'Foo#Bar' is not recognized as a name [...]
347+
348+
PS> Foo<#Bar#>
349+
Foo<#Bar#>: The term 'Foo<#Bar#>' is not recognized as a name [...]
350+
```
351+
352+
<!-- link references -->
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.md#double-quoted-strings
359+
[07]: about_quoting_rules.md#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
372+
[20]: xref:Microsoft.PowerShell.Utility.Export-Csv
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

Comments
 (0)