Cleaning Your PowerShell House
As you develop your PowerShell scripting skills, you will advance from creating simple scripts to writing advanced functions that consume pipeline input and write objects to the pipeline. These functions are no different than cmdlets like Get-Process
, except that you wrote them. You create an advanced PowerShell function by including three named script blocks.
Function Get-Data {
[CmdletBinding()]
Param()
Begin {
#code that runs once before pipeline processing
}
Process {
#code that runs for each pipelined object
}
End {
#code that runs once after pipeline processing
}
}
The Begin and End scriptblocks are optional, but I always include them even if they are empty like this:
Begin {
# not used
}
Process {
#my code here
}
End {
# not used
}
Want to read the full issue?