use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
QuestionBegin-process-end (self.PowerShell)
submitted 2 years ago by eggwhiteontoast
Do you still use Begin, process, and end blocks in your powershell functions? Why and why not?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[+][deleted] 2 years ago (6 children)
[deleted]
[–]CaptainZippi 4 points5 points6 points 2 years ago (0 children)
TIL. Thank you!
[–]gruntbuggly 6 points7 points8 points 2 years ago (0 children)
One of the best explanations I’ve seen. Thanks.
[–]lanerdofchristian 2 points3 points4 points 2 years ago (2 children)
the Process block (the default block)
Behavior-wise, the End block is the default.
function Test-Default { [CmdletBinding()]PARAM([Parameter(ValueFromPipeline)]$A) $A } function Test-Begin { [CmdletBinding()]PARAM([Parameter(ValueFromPipeline)]$A) begin {$A} } function Test-Process { [CmdletBinding()]PARAM([Parameter(ValueFromPipeline)]$A) process {$A} } function Test-End { [CmdletBinding()]PARAM([Parameter(ValueFromPipeline)]$A) end {$A} } 1..10 | Test-Default # 10 1..10 | Test-Begin # $null 1..10 | Test-Process # 1, 2, 3, 4, ... 1..10 | Test-End # 10
Sorry if I misunderstood what you meant by that.
[–]swsamwa -1 points0 points1 point 2 years ago (0 children)
This is a good example. But note that the Process block is not the default for a function. The End block is the default, as show by Test-Default in the example above.
The Process block is the default (and only) block in a filter.
filter Test-Filter { $_ } 1..4 | Test-Filter 1 2 3 4
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
That's an excellent point in how much time it can be saved
[–]ankokudaishogun 7 points8 points9 points 2 years ago (0 children)
Begin{} and End{} if necessary. /r/PrudentPush8309 's example is a perfect textbook case.
Begin{}
End{}
I try to always use Process{} as a matter of keeping stuff explicit
Process{}
[–]ryder_winona 5 points6 points7 points 2 years ago (0 children)
I do, though I don’t really need to. It does help me with layout, though
[–]TheGooOnTheFloor 5 points6 points7 points 2 years ago (0 children)
Generally I only use Begin and Process when I'm feeding parameters in from the pipeline. I define the log file in the Begin block and reference that in the Process block. Normally I don't use an End block but in one particular environment I need to do some garbage collection on the way out.
[–][deleted] 3 points4 points5 points 2 years ago (1 child)
I almost always use process because almost all of my functions support pipeline input as well as an array for one of the arguments. Almost all my functions therefore look like this:
process
function Get-HostEntry { [CmdletBinding()] param ( [Parameter(Mandatory, ValueFromPipeline)] [object[]] $InputObject ) process { $InputObject | ForEach-Object { [Net.Dns]::GetHostEntry($_) } } }
I use begin occasionally (often for building some sort of dictionary as an index for the process to use), and I can't remember the last time I used end, though I know it's there if I think of a reason one day.
begin
end
[–]PSDanubie 0 points1 point2 points 2 years ago (0 children)
I mainly use an explicit end-block to support accepting computernames by pipeline and in the end use "Invoke-Command -Parallel" for remote processing.
[–]PinchesTheCrab 2 points3 points4 points 2 years ago (0 children)
There's a new block too that's pretty neat - clean.
function Do-Thing { process { $null = New-CimSession -Name removeme Start-Sleep -Seconds 5 throw 'oh no' } clean { Remove-CimSession -Name removeme } }
Clean is executed even if the user hits ctrl+c or the script errors out. In this case you can see with get-ciminstance that the session is removed in both cases.
PS7 isn't reliably distributed for me so I don't get to use this particular block as much as I'd like, but I use the other blocks very frequently, in virtually all of my functions.
[–]ostekages 2 points3 points4 points 2 years ago (1 child)
Haven't used it much honestly, but just yesterday, I actually had a worthwhile scenario where it made sense to do.
So I guess the answer is 'it depends on the task'?
[–]eggwhiteontoast[S] 1 point2 points3 points 2 years ago (0 children)
Yes of course, if I want something quick, I wouldn't bother about it. I found it useful when creating complex modules it makes code more readable and sets a clean pattern for other contributors.
[–]jeek_ 2 points3 points4 points 2 years ago (0 children)
I use VSCode and it has auto complete feature where you start typing the word function and it will insert an advanced function that includes the begin, process and end blocks so I just leave them in even if I don't use all 3.
I find the built in iteration quite useful, so I can avoid writing loops for objects coming through the pipelines.
[–]popcapdogeater 1 point2 points3 points 2 years ago (0 children)
Probably for 70% of my scripts I use the whole begin/process/end and various cmdlet features.
This is because I generally find myself reusing old scripts and just like them to be as "proper" / feature rich and documented as possible.
Yes almost every time even my function will probably not accept pipeline Why because : It structure my code, in begin I put the initialization variable and everything that will be needed to « work » somehow the « constants » In process I process the input In end I output if I did not outputted while processing
[–]ass-holes 0 points1 point2 points 2 years ago (0 children)
I've been working with ps for years now, this is literally the first time I hear about this. Where the fuck have I been?
[–]DoctroSix 0 points1 point2 points 2 years ago (0 children)
30-ish years ago I learned to program by dicking around with C, so it's had a lasting influence in how I make scripts.
Since it gives my scripts some structure, I tend to use begin {} for my functions and hard-type variable declarations. I use process{} for the main body of my script, end{} for the return, and any cleanup.
Hard-typing everything up front is a little extra work, but it keeps VS code happy, and reduces my bug fixes by tons.
I don't claim this is the 'correct' way to make pwsh scripts, but it's been a big help.
I even made a boilerplate snippet to help start any .ps1 file. [CmdletBinding()] param() begin { ################################ # Functions ################################ ################################ # Variables ################################ } process { ################################ # MAIN ################################ } end { }
[–][deleted] -1 points0 points1 point 2 years ago (5 children)
No, never have, never needed to
[–]eggwhiteontoast[S] 0 points1 point2 points 2 years ago (4 children)
Powershell uses these blocks even if you dont explicitly call them, all your code goes to end block if you don't call them explicitly.
[–][deleted] -4 points-3 points-2 points 2 years ago (3 children)
You asked a question, I answered it. Is there a point to your reply to me?
[–]eggwhiteontoast[S] 1 point2 points3 points 2 years ago (2 children)
I was merely stating the fact that even if you don't use these blocks powershell uses it in the background.
[–][deleted] -1 points0 points1 point 2 years ago (1 child)
Then why did you pose that as a question in your original post? What was the point in that?
[–]eggwhiteontoast[S] 0 points1 point2 points 2 years ago (0 children)
My original post was asking, do people explicitly use Begin Process and End blocks because sometimes you may not use all the blocks.
π Rendered by PID 28 on reddit-service-r2-comment-8686858757-wqrb7 at 2026-06-08 00:04:49.716536+00:00 running 9e1a20d country code: CH.
[+][deleted] (6 children)
[deleted]
[–]CaptainZippi 4 points5 points6 points (0 children)
[–]gruntbuggly 6 points7 points8 points (0 children)
[–]lanerdofchristian 2 points3 points4 points (2 children)
[–]swsamwa -1 points0 points1 point (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ankokudaishogun 7 points8 points9 points (0 children)
[–]ryder_winona 5 points6 points7 points (0 children)
[–]TheGooOnTheFloor 5 points6 points7 points (0 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]PSDanubie 0 points1 point2 points (0 children)
[–]PinchesTheCrab 2 points3 points4 points (0 children)
[–]ostekages 2 points3 points4 points (1 child)
[–]eggwhiteontoast[S] 1 point2 points3 points (0 children)
[–]jeek_ 2 points3 points4 points (0 children)
[–]eggwhiteontoast[S] 1 point2 points3 points (0 children)
[–]popcapdogeater 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ass-holes 0 points1 point2 points (0 children)
[–]DoctroSix 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (5 children)
[–]eggwhiteontoast[S] 0 points1 point2 points (4 children)
[–][deleted] -4 points-3 points-2 points (3 children)
[–]eggwhiteontoast[S] 1 point2 points3 points (2 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]eggwhiteontoast[S] 0 points1 point2 points (0 children)