11---
22description : Describes easier, more natural-language ways of scripting filters for collections of objects.
33Locale : en-US
4- ms.date : 06/09/2017
4+ ms.date : 04/26/2024
55online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_simplified_syntax?view=powershell-5.1&WT.mc_id=ps-gethelp
66schema : 2.0.0
77title : about Simplified Syntax
88---
9-
109# about_Simplified_Syntax
1110
1211## Short description
13-
1412Describes easier, more natural-language ways of scripting filters for
1513collections of objects.
1614
1715## Long description
1816
1917Simplified 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
2523You can use a method on the members of a collection (most commonly, an array)
2624without 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
4966While 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
6182Get-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
7092Get-ChildItem /home -Directory | foreach GetFiles .*
0 commit comments