22external help file : System.Management.Automation.dll-Help.xml
33Locale : en-US
44Module Name : Microsoft.PowerShell.Core
5- ms.date : 01/13/2023
5+ ms.date : 04/26/2024
66online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-5.1&WT.mc_id=ps-gethelp
77schema : 2.0.0
88title : ForEach-Object
@@ -49,7 +49,7 @@ command.
4949 [ about_functions] ( about/about_functions.md#piping-objects-to-functions ) .
5050
5151 > [ !NOTE]
52- > The script blocks run in the caller's scope. Therefore the blocks have access to variables in
52+ > The script blocks run in the caller's scope. Therefore, the blocks have access to variables in
5353 > that scope and can create new variables that persist in that scope after the cmdlet completes.
5454
5555- ** Operation statement** . You can also write an operation statement, which is much more like
@@ -86,7 +86,7 @@ Get-ChildItem $PSHOME |
8686 ForEach-Object -Process {if (!$_.PSIsContainer) {$_.Name; $_.Length / 1024; " " }}
8787```
8888
89- If the object is not a directory, the script block gets the name of the file, divides the value of
89+ If the object isn't a directory, the script block gets the name of the file, divides the value of
9090its ** Length** property by 1024, and adds a space (" ") to separate it from the next entry. The
9191cmdlet uses the ** PSISContainer** property to determine whether an object is a directory.
9292
@@ -96,16 +96,17 @@ This example writes the 1000 most recent events from the System event log to a t
9696current time is displayed before and after processing the events.
9797
9898``` powershell
99- $Events = Get-EventLog -LogName System -Newest 1000
100- $events | ForEach-Object -Begin {Get-Date} -Process {Out-File -FilePath Events.txt -Append -InputObject $_.Message} -End {Get-Date}
99+ Get-EventLog -LogName System -Newest 1000 |
100+ ForEach-Object -Begin {Get-Date} -Process {
101+ Out-File -FilePath Events.txt -Append -InputObject $_.Message
102+ } -End {Get-Date}
101103```
102104
103- ` Get-EventLog ` gets the 1000 most recent events from the System event log and stores them in the
104- ` $Events ` variable. ` $Events ` is then piped to the ` ForEach-Object ` cmdlet. The ** Begin** parameter
105- displays the current date and time. Next, the ** Process** parameter uses the ` Out-File ` cmdlet to
106- create a text file that is named events.txt and stores the message property of each of the events in
107- that file. Last, the ** End** parameter is used to display the date and time after all the processing
108- has completed.
105+ ` Get-EventLog ` gets the 1000 most recent events from the System event log and pipes them to the
106+ ` ForEach-Object ` cmdlet. The ** Begin** parameter displays the current date and time. Next, the
107+ ** Process** parameter uses the ` Out-File ` cmdlet to create a text file that's named events.txt and
108+ stores the message property of each of the events in that file. Last, the ** End** parameter is used
109+ to display the date and time after all the processing has completed.
109110
110111### Example 4: Change the value of a Registry key
111112
@@ -114,13 +115,15 @@ This example changes the value of the **RemotePath** registry entry in all the s
114115
115116``` powershell
116117Get-ItemProperty -Path HKCU:\Network\* |
117- ForEach-Object {Set-ItemProperty -Path $_.PSPath -Name RemotePath -Value $_.RemotePath.ToUpper();}
118+ ForEach-Object {
119+ Set-ItemProperty -Path $_.PSPath -Name RemotePath -Value $_.RemotePath.ToUpper()
120+ }
118121```
119122
120123You can use this format to change the form or content of a registry entry value.
121124
122125Each subkey in the ** Network** key represents a mapped network drive that reconnects at sign on. The
123- ** RemotePath** entry contains the UNC path of the connected drive. For example, if you map the E:
126+ ** RemotePath** entry contains the UNC path of the connected drive. For example, if you map the ` E: `
124127drive to ` \\Server\Share ` , an ** E** subkey is created in ` HKCU:\Network ` with the ** RemotePath**
125128registry value set to ` \\Server\Share ` .
126129
@@ -129,7 +132,7 @@ The command uses the `Get-ItemProperty` cmdlet to get all the subkeys of the **N
129132the ` Set-ItemProperty ` command, the path is the value of the ** PSPath** property of the registry
130133key. This is a property of the Microsoft .NET Framework object that represents the registry key, not
131134a registry entry. The command uses the ** ToUpper()** method of the ** RemotePath** value, which is a
132- string ( REG_SZ) .
135+ string ** REG_SZ** .
133136
134137Because ` Set-ItemProperty ` is changing the property of each key, the ` ForEach-Object ` cmdlet is
135138required to access the property.
@@ -155,7 +158,7 @@ a value for `$null` as it does for other objects piped to it.
155158
156159### Example 6: Get property values
157160
158- This example gets the value of the ** Path** property of all installed PowerShell modules by using
161+ This example gets the value of the ** Path** property of all installed PowerShell modules using
159162the ** MemberName** parameter of the ` ForEach-Object ` cmdlet.
160163
161164``` powershell
@@ -177,9 +180,12 @@ The commands call the **Split** method of strings. The three commands use differ
177180are equivalent and interchangeable. The output is the same for all three cases.
178181
179182``` powershell
180- "Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | ForEach-Object {$_.Split(".")}
181- "Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | ForEach-Object -MemberName Split -ArgumentList "."
182- "Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" | Foreach Split "."
183+ "Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" |
184+ ForEach-Object {$_.Split(".")}
185+ "Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" |
186+ ForEach-Object -MemberName Split -ArgumentList "."
187+ "Microsoft.PowerShell.Core", "Microsoft.PowerShell.Host" |
188+ Foreach Split "."
183189```
184190
185191``` Output
@@ -204,7 +210,7 @@ the **MemberName** and **ArgumentList** parameters, which are optional.
204210### Example 8: Using ForEach-Object with two script blocks
205211
206212In this example, we pass two script blocks positionally. All the script blocks bind to the
207- ** Process** parameter. However, they are treated as if they had been passed to the ** Begin** and
213+ ** Process** parameter. However, they're treated as if they had been passed to the ** Begin** and
208214** Process** parameters.
209215
210216``` powershell
@@ -220,7 +226,7 @@ process
220226### Example 9: Using ForEach-Object with more than two script blocks
221227
222228In this example, we pass four script blocks positionally. All the script blocks bind to the
223- ** Process** parameter. However, they are treated as if they had been passed to the ** Begin** ,
229+ ** Process** parameter. However, they're treated as if they had been passed to the ** Begin** ,
224230** Process** , and ** End** parameters.
225231
226232``` powershell
238244
239245> [ !NOTE]
240246> The first script block is always mapped to the ` begin ` block, the last block is mapped to the
241- > ` end ` block, and the blocks in between are all mapped to the ` process ` block.
247+ > ` end ` block, and the two middle blocks are mapped to the ` process ` block.
242248
243249### Example 10: Run multiple script blocks for each pipeline item
244250
@@ -324,8 +330,8 @@ the objects.
324330
325331When you use the **InputObject** parameter with `ForEach-Object`, instead of piping command results
326332to `ForEach-Object`, the **InputObject** value is treated as a single object. This is true even if
327- the value is a collection that is the result of a command, such as `-InputObject (Get-Process)`.
328- Because **InputObject** cannot return individual properties from an array or collection of objects,
333+ the value is a collection that's the result of a command, such as `-InputObject (Get-Process)`.
334+ Because **InputObject** can't return individual properties from an array or collection of objects,
329335we recommend that if you use `ForEach-Object` to perform operations on a collection of objects for
330336those objects that have specific values in defined properties, you use `ForEach-Object` in the
331337pipeline, as shown in the examples in this topic.
@@ -344,7 +350,8 @@ Accept wildcard characters: False
344350
345351# ## -MemberName
346352
347- Specifies the property to get or the method to call.
353+ Specifies the name of the member property to get or the member method to call. The members must be
354+ instance members, not static members.
348355
349356Wildcard characters are permitted, but work only if the resulting string resolves to a unique value.
350357For example, if you run `Get-Process | ForEach -MemberName *Name`, the wildcard pattern matches more
@@ -366,15 +373,15 @@ Accept wildcard characters: True
366373
367374# ## -Process
368375
369- Specifies the operation that is performed on each input object. This script block is run for every
376+ Specifies the operation that's performed on each input object. This script block is run for every
370377object in the pipeline. For more information about the `process` block, see
371378[about_Functions](about/about_functions.md#piping-objects-to-functions).
372379
373380When you provide multiple script blocks to the **Process** parameter, the first script block is
374381always mapped to the `begin` block. If there are only two script blocks, the second block is mapped
375382to the `process` block. If there are three or more script blocks, first script block is always
376- mapped to the `begin` block, the last block is mapped to the `end` block, and the blocks in between
377- are all mapped to the `process` block.
383+ mapped to the `begin` block, the last block is mapped to the `end` block, and the middle blocks are
384+ mapped to the `process` block.
378385
379386` ` ` yaml
380387Type: System.Management.Automation.ScriptBlock[]
@@ -390,7 +397,7 @@ Accept wildcard characters: False
390397
391398# ## -RemainingScripts
392399
393- Specifies all script blocks that are not taken by the **Process** parameter.
400+ Specifies all script blocks that aren't taken by the **Process** parameter.
394401
395402This parameter was introduced in Windows PowerShell 3.0.
396403
@@ -424,7 +431,7 @@ Accept wildcard characters: False
424431
425432# ## -WhatIf
426433
427- Shows what would happen if the cmdlet runs. The cmdlet is not run.
434+ Shows what would happen if the cmdlet runs. The cmdlet isn't run.
428435
429436` ` ` yaml
430437Type: System.Management.Automation.SwitchParameter
0 commit comments