Reminder to self: script blocks inside .ForEach calls can have Begin/Process/End blocks
Posted by jpluimers on 2021/10/21
I need to write some tests for this, but it looks like you can use the keywords Begin
/Process
/End
with code blocks when the script block is inside a .ForEach
member call.
The behaviour seems to be the same as if these blocks are part of a function that executes inside a pipeline (Begin
and End
are executed once; Process
is executed for each item in the pipeline).
It’s hard to Google on this, as all hits of all queries I tried got me into these keywords in the context of functions.
The below links are on my reading list.
Microsoft documentation:
- [WayBack] about_Arrays: Methods of Arrays – PowerShell | Microsoft Docs
- [WayBack] ForEach-Object
- [WayBack] about_Script_Blocks – PowerShell | Microsoft Docs
- [WayBack] about_Functions – PowerShell | Microsoft Docs
- [WayBack] about_Functions_Advanced – PowerShell | Microsoft Docs
- [WayBack] about_Functions_Advanced_Methods – PowerShell | Microsoft Docs
SS64 docs (which has guidance on which of the 3 foreach constructs to use when):
- [WayBack] ForEach method – PowerShell – SS64.com
- [WayBack] ForEach – PowerShell – SS64.com on the foreach statement)
- [WayBack] ForEach-Object % – PowerShell – SS64.com (aliased
ForEach
and%
) - [WayBack] Function Begin Process End – PowerShell – SS64.com[WayBack] Powershell Arrays – PowerShell – SS64.com
- [WayBack] Scriptblocks – PowerShell – SS64.com
Social media and blog posts:
- [WayBack] PowerShell:
$array.ForEach({})
| Saved Keystrokes - [WayBack] PowerShell 4.0 Where and ForEach Method Syntax – TechNet Articles – United States (English) – TechNet Wiki
- [WayBack] Extending the Reach of ForEach in PowerShell — Microsoft Certified Professional Magazine Online
- [WayBack] PowerShell functions: begin, process and end blocks
- [WayBack] $input gotchas | Dmitry’s Blog: Cloud, PowerShell and beyond
- [WayBack] Advanced PowerShell Functions: Begin to Process to End – SAPIEN Blog (shows what happens when nesting functions having these blocks)
- [WayBack] PowerShell – Begin Process End – CodeAndKeep.Com – Code and keep calm…
- [WayBack] Implement Pipeline Support by making proper use of begin, process and end blocks in PowerShell functions – mohitgoyal.co
StackOverflow entries:
- [WayBack] powershell – How does begin/process/end save the need for foreach? Still needed for the parameter isn’t it? – Stack Overflow
- [WayBack] PowerShell: the mysterious -RemainingScripts parameter of ForEach-Object – Stack Overflow
- [WayBack] powershell – Use Begin, Process, End in Scriptblock – Stack Overflow
- [WayBack] Difference between ForEach and ForEach-Object in powershell – Stack Overflow
- [WayBack] PowerShell: How to get count of piped collection? – Stack Overflow
- [WayBack] powershell – Nested function inside a function with begin/process/end blocks? – Stack Overflow
- [WayBack] PowerShell Pipeline iteration – Stack Overflow
- [WayBack] powershell – How to query registry values skipping the PS* ones – Stack Overflow (thanks [WayBack] User Keith Miller – Stack Overflow / [WayBack] Keith A. Miller – Microsoft Community for bringing this to my attention)
I know this is truly old, but here’s a function I added to my Profile to facilitate registry exploration:
Function Get-KeyProperty { Param( [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [ValidateScript( {(Resolve-Path $_).Provider.Name -Like 'Registry'} )] [String[]] $Path, [String[]] $Name=@("*") ) Process { $Name.ForEach({ (Get-Item $Path).Property -like $_}).ForEach({ Begin {$Hash = @{}} Process { $Hash += @{ $_ = (Get-Item $Path).GetValue($_)} } End {[PSCustomObject]$Hash} }) }}
…
–jeroen
Leave a Reply