Skip to content

Commit 67c20cf

Browse files
authored
Clarify which positional parameters are used in the examples (#11056)
1 parent ff081d3 commit 67c20cf

5 files changed

Lines changed: 245 additions & 127 deletions

File tree

reference/5.1/Microsoft.PowerShell.Core/About/about_Simplified_Syntax.md

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
---
22
description: Describes easier, more natural-language ways of scripting filters for collections of objects.
33
Locale: en-US
4-
ms.date: 06/09/2017
4+
ms.date: 04/26/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_simplified_syntax?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about Simplified Syntax
88
---
9-
109
# about_Simplified_Syntax
1110

1211
## Short description
13-
1412
Describes easier, more natural-language ways of scripting filters for
1513
collections of objects.
1614

1715
## Long description
1816

1917
Simplified syntax, introduced in Windows PowerShell 3.0, lets you build some
20-
filter commands without using script blocks. The simplified syntax more
21-
closely resembles natural language, and is primarily useful with collections
22-
of objects that get piped into commands `Where-Object` and `ForEach-Object` and
23-
their corresponding aliases `where` and `foreach`.
18+
filter commands without using script blocks. The simplified syntax more closely
19+
resembles natural language, and is primarily useful with collections of objects
20+
that get piped into commands `Where-Object` and `ForEach-Object` or their
21+
corresponding aliases `where` and `foreach`.
2422

2523
You can use a method on the members of a collection (most commonly, an array)
2624
without referring to the automatic variable `$_` inside a script block.
@@ -30,41 +28,65 @@ Consider the following two invocations:
3028
### Standard Syntax
3129

3230
```powershell
33-
dir Cert:\LocalMachine\Root | where { $_.FriendlyName -eq 'Verisign' }
34-
dir Cert:\ -Recurse | foreach { $_.GetKeyAlgorithm() }
31+
Get-ChildItem Cert:\LocalMachine\Root |
32+
Where-Object -FilterScript { $_.FriendlyName -eq 'Verisign' }
33+
Get-ChildItem Cert:\ -Recurse |
34+
ForEach-Object -FilterScript { $_.GetKeyAlgorithm() }
3535
```
3636

37+
> [!NOTE]
38+
> In the second command, the `GetKeyAlgorithm` method is called on each object
39+
> in the collection. If the object received from the pipeline doesn't have a
40+
> `GetKeyAlgorithm` method, the command produces an error.
41+
3742
### Simplified syntax
3843

39-
Under the simplified syntax, comparison operators that work on members of objects in a
40-
collection are treated as parameters. You can invoke a method on objects in a
41-
collection without referring to the automatic variable `$_` inside a script block.
42-
Compare the following two invocations to those of the previous example:
44+
Under the simplified syntax, comparison operators that work on members of
45+
objects in a collection are implemented as parameters. Also, you can invoke a
46+
method on objects in a collection without referring to the automatic variable
47+
`$_` inside a script block. Compare the following two invocations to the
48+
standard syntax examples:
49+
50+
```powershell
51+
Get-ChildItem Cert:\LocalMachine\Root |
52+
Where-Object -Property FriendlyName -EQ 'Verisign'
53+
Get-ChildItem Cert:\ -Recurse |
54+
ForEach-Object -MemberName GetKeyAlgorithm
55+
```
56+
57+
Since the **Property** and **MemberName** parameters are positional, you can
58+
omit them from the command. Using aliases, you can further simplify the
59+
commands:
4360

4461
```powershell
45-
dir Cert:\LocalMachine\Root | where FriendlyName -eq 'Verisign'
46-
dir Cert:\ -Recurse | foreach GetKeyAlgorithm
62+
dir Cert:\LocalMachine\Root | Where FriendlyName -EQ 'Verisign'
63+
dir Cert:\ -Recurse | ForEach GetKeyAlgorithm
4764
```
4865

4966
While both syntaxes work, the simplified syntax returns results without
50-
referring to the automatic variable `$_` inside a script block.
51-
The method name `GetKeyAlgorithm` is treated as a parameter of `ForEach-Object`.
52-
The second command returns the same results, but without errors,
53-
because the simplified syntax does not attempt to return results for items
54-
for which the specified argument did not apply.
67+
referring to the automatic variable `$_` inside a script block. The simplified
68+
syntax reads more like a natural language statement and can be easier to
69+
understand.
70+
71+
The method name `GetKeyAlgorithm` is passed as an argument for the
72+
**MemberName** parameter of `ForEach-Object`. When you invoke the method using
73+
the simplified syntax, the method is called for each object in pipeline only if
74+
that object has that method. Therefore, you get the same results, but without
75+
errors.
5576

56-
In this example, the `Process` property `Description` is passed as the member name
57-
parameter to the `ForEach-Object` command. The results are descriptions of active
58-
processes.
77+
In the next example, `Description` is passed to the **MemberName** parameter of
78+
`ForEach-Object`. The command displays the description of each
79+
**System.Diagnostics.Process** object returned by `Get-Process`.
5980

6081
```powershell
6182
Get-Process | foreach Description
6283
```
6384

64-
In this example, the `DirectoryInfo` method `GetFiles` is passed as the member name
65-
parameter of the `ForEach-Object` command.
66-
The method is called with the search pattern parameter `.*`.
67-
The results are `FileInfo` records for all Unix-style hidden files in user home directories.
85+
In this example, the method name `GetFiles` is passed to the **MemberName**
86+
parameter of the `ForEach-Object` command. The `.*` value is passed to the
87+
**ArgumentList** parameter. The `GetFiles()` method is called with the search
88+
pattern parameter `.*` for each **System.IO.DirectoryInfo** object returned by
89+
`Get-ChildItem`.
6890

6991
```powershell
7092
Get-ChildItem /home -Directory | foreach GetFiles .*

reference/7.2/Microsoft.PowerShell.Core/About/about_Simplified_Syntax.md

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes easier, more natural-language ways of scripting filters for collections of objects.
33
Locale: en-US
4-
ms.date: 06/09/2017
4+
ms.date: 04/26/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_simplified_syntax?view=powershell-7.2&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about Simplified Syntax
@@ -15,10 +15,10 @@ collections of objects.
1515
## Long description
1616

1717
Simplified syntax, introduced in Windows PowerShell 3.0, lets you build some
18-
filter commands without using script blocks. The simplified syntax more
19-
closely resembles natural language, and is primarily useful with collections
20-
of objects that get piped into commands `Where-Object` and `ForEach-Object` and
21-
their corresponding aliases `where` and `foreach`.
18+
filter commands without using script blocks. The simplified syntax more closely
19+
resembles natural language, and is primarily useful with collections of objects
20+
that get piped into commands `Where-Object` and `ForEach-Object` or their
21+
corresponding aliases `where` and `foreach`.
2222

2323
You can use a method on the members of a collection (most commonly, an array)
2424
without referring to the automatic variable `$_` inside a script block.
@@ -28,41 +28,65 @@ Consider the following two invocations:
2828
### Standard Syntax
2929

3030
```powershell
31-
dir Cert:\LocalMachine\Root | where { $_.FriendlyName -eq 'Verisign' }
32-
dir Cert:\ -Recurse | foreach { $_.GetKeyAlgorithm() }
31+
Get-ChildItem Cert:\LocalMachine\Root |
32+
Where-Object -FilterScript { $_.FriendlyName -eq 'Verisign' }
33+
Get-ChildItem Cert:\ -Recurse |
34+
ForEach-Object -FilterScript { $_.GetKeyAlgorithm() }
3335
```
3436

37+
> [!NOTE]
38+
> In the second command, the `GetKeyAlgorithm` method is called on each object
39+
> in the collection. If the object received from the pipeline doesn't have a
40+
> `GetKeyAlgorithm` method, the command produces an error.
41+
3542
### Simplified syntax
3643

37-
Under the simplified syntax, comparison operators that work on members of objects in a
38-
collection are treated as parameters. You can invoke a method on objects in a
39-
collection without referring to the automatic variable `$_` inside a script block.
40-
Compare the following two invocations to those of the previous example:
44+
Under the simplified syntax, comparison operators that work on members of
45+
objects in a collection are implemented as parameters. Also, you can invoke a
46+
method on objects in a collection without referring to the automatic variable
47+
`$_` inside a script block. Compare the following two invocations to the
48+
standard syntax examples:
49+
50+
```powershell
51+
Get-ChildItem Cert:\LocalMachine\Root |
52+
Where-Object -Property FriendlyName -EQ 'Verisign'
53+
Get-ChildItem Cert:\ -Recurse |
54+
ForEach-Object -MemberName GetKeyAlgorithm
55+
```
56+
57+
Since the **Property** and **MemberName** parameters are positional, you can
58+
omit them from the command. Using aliases, you can further simplify the
59+
commands:
4160

4261
```powershell
43-
dir Cert:\LocalMachine\Root | where FriendlyName -eq 'Verisign'
44-
dir Cert:\ -Recurse | foreach GetKeyAlgorithm
62+
dir Cert:\LocalMachine\Root | Where FriendlyName -EQ 'Verisign'
63+
dir Cert:\ -Recurse | ForEach GetKeyAlgorithm
4564
```
4665

4766
While both syntaxes work, the simplified syntax returns results without
48-
referring to the automatic variable `$_` inside a script block.
49-
The method name `GetKeyAlgorithm` is treated as a parameter of `ForEach-Object`.
50-
The second command returns the same results, but without errors,
51-
because the simplified syntax does not attempt to return results for items
52-
for which the specified argument did not apply.
67+
referring to the automatic variable `$_` inside a script block. The simplified
68+
syntax reads more like a natural language statement and can be easier to
69+
understand.
70+
71+
The method name `GetKeyAlgorithm` is passed as an argument for the
72+
**MemberName** parameter of `ForEach-Object`. When you invoke the method using
73+
the simplified syntax, the method is called for each object in pipeline only if
74+
that object has that method. Therefore, you get the same results, but without
75+
errors.
5376

54-
In this example, the `Process` property `Description` is passed as the member name
55-
parameter to the `ForEach-Object` command. The results are descriptions of active
56-
processes.
77+
In the next example, `Description` is passed to the **MemberName** parameter of
78+
`ForEach-Object`. The command displays the description of each
79+
**System.Diagnostics.Process** object returned by `Get-Process`.
5780

5881
```powershell
5982
Get-Process | foreach Description
6083
```
6184

62-
In this example, the `DirectoryInfo` method `GetFiles` is passed as the member name
63-
parameter of the `ForEach-Object` command.
64-
The method is called with the search pattern parameter `.*`.
65-
The results are `FileInfo` records for all Unix-style hidden files in user home directories.
85+
In this example, the method name `GetFiles` is passed to the **MemberName**
86+
parameter of the `ForEach-Object` command. The `.*` value is passed to the
87+
**ArgumentList** parameter. The `GetFiles()` method is called with the search
88+
pattern parameter `.*` for each **System.IO.DirectoryInfo** object returned by
89+
`Get-ChildItem`.
6690

6791
```powershell
6892
Get-ChildItem /home -Directory | foreach GetFiles .*

reference/7.3/Microsoft.PowerShell.Core/About/about_Simplified_Syntax.md

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes easier, more natural-language ways of scripting filters for collections of objects.
33
Locale: en-US
4-
ms.date: 06/09/2017
4+
ms.date: 04/26/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_simplified_syntax?view=powershell-7.3&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about Simplified Syntax
@@ -15,10 +15,10 @@ collections of objects.
1515
## Long description
1616

1717
Simplified syntax, introduced in Windows PowerShell 3.0, lets you build some
18-
filter commands without using script blocks. The simplified syntax more
19-
closely resembles natural language, and is primarily useful with collections
20-
of objects that get piped into commands `Where-Object` and `ForEach-Object` and
21-
their corresponding aliases `where` and `foreach`.
18+
filter commands without using script blocks. The simplified syntax more closely
19+
resembles natural language, and is primarily useful with collections of objects
20+
that get piped into commands `Where-Object` and `ForEach-Object` or their
21+
corresponding aliases `where` and `foreach`.
2222

2323
You can use a method on the members of a collection (most commonly, an array)
2424
without referring to the automatic variable `$_` inside a script block.
@@ -28,41 +28,65 @@ Consider the following two invocations:
2828
### Standard Syntax
2929

3030
```powershell
31-
dir Cert:\LocalMachine\Root | where { $_.FriendlyName -eq 'Verisign' }
32-
dir Cert:\ -Recurse | foreach { $_.GetKeyAlgorithm() }
31+
Get-ChildItem Cert:\LocalMachine\Root |
32+
Where-Object -FilterScript { $_.FriendlyName -eq 'Verisign' }
33+
Get-ChildItem Cert:\ -Recurse |
34+
ForEach-Object -FilterScript { $_.GetKeyAlgorithm() }
3335
```
3436

37+
> [!NOTE]
38+
> In the second command, the `GetKeyAlgorithm` method is called on each object
39+
> in the collection. If the object received from the pipeline doesn't have a
40+
> `GetKeyAlgorithm` method, the command produces an error.
41+
3542
### Simplified syntax
3643

37-
Under the simplified syntax, comparison operators that work on members of objects in a
38-
collection are treated as parameters. You can invoke a method on objects in a
39-
collection without referring to the automatic variable `$_` inside a script block.
40-
Compare the following two invocations to those of the previous example:
44+
Under the simplified syntax, comparison operators that work on members of
45+
objects in a collection are implemented as parameters. Also, you can invoke a
46+
method on objects in a collection without referring to the automatic variable
47+
`$_` inside a script block. Compare the following two invocations to the
48+
standard syntax examples:
49+
50+
```powershell
51+
Get-ChildItem Cert:\LocalMachine\Root |
52+
Where-Object -Property FriendlyName -EQ 'Verisign'
53+
Get-ChildItem Cert:\ -Recurse |
54+
ForEach-Object -MemberName GetKeyAlgorithm
55+
```
56+
57+
Since the **Property** and **MemberName** parameters are positional, you can
58+
omit them from the command. Using aliases, you can further simplify the
59+
commands:
4160

4261
```powershell
43-
dir Cert:\LocalMachine\Root | where FriendlyName -eq 'Verisign'
44-
dir Cert:\ -Recurse | foreach GetKeyAlgorithm
62+
dir Cert:\LocalMachine\Root | Where FriendlyName -EQ 'Verisign'
63+
dir Cert:\ -Recurse | ForEach GetKeyAlgorithm
4564
```
4665

4766
While both syntaxes work, the simplified syntax returns results without
48-
referring to the automatic variable `$_` inside a script block.
49-
The method name `GetKeyAlgorithm` is treated as a parameter of `ForEach-Object`.
50-
The second command returns the same results, but without errors,
51-
because the simplified syntax does not attempt to return results for items
52-
for which the specified argument did not apply.
67+
referring to the automatic variable `$_` inside a script block. The simplified
68+
syntax reads more like a natural language statement and can be easier to
69+
understand.
70+
71+
The method name `GetKeyAlgorithm` is passed as an argument for the
72+
**MemberName** parameter of `ForEach-Object`. When you invoke the method using
73+
the simplified syntax, the method is called for each object in pipeline only if
74+
that object has that method. Therefore, you get the same results, but without
75+
errors.
5376

54-
In this example, the `Process` property `Description` is passed as the member name
55-
parameter to the `ForEach-Object` command. The results are descriptions of active
56-
processes.
77+
In the next example, `Description` is passed to the **MemberName** parameter of
78+
`ForEach-Object`. The command displays the description of each
79+
**System.Diagnostics.Process** object returned by `Get-Process`.
5780

5881
```powershell
5982
Get-Process | foreach Description
6083
```
6184

62-
In this example, the `DirectoryInfo` method `GetFiles` is passed as the member name
63-
parameter of the `ForEach-Object` command.
64-
The method is called with the search pattern parameter `.*`.
65-
The results are `FileInfo` records for all Unix-style hidden files in user home directories.
85+
In this example, the method name `GetFiles` is passed to the **MemberName**
86+
parameter of the `ForEach-Object` command. The `.*` value is passed to the
87+
**ArgumentList** parameter. The `GetFiles()` method is called with the search
88+
pattern parameter `.*` for each **System.IO.DirectoryInfo** object returned by
89+
`Get-ChildItem`.
6690

6791
```powershell
6892
Get-ChildItem /home -Directory | foreach GetFiles .*

0 commit comments

Comments
 (0)