PowerShell Functions 101
One of the content suggestions I received recently asked for content on writing PowerShell functions. Specifically, the reader wanted to see how to write a basic function and then compare it to an advanced version of the function. I write about functions and the scripting process all the time, but there are always new readers who may not have gone through the back catalog of articles. And repetition is always good when it comes to learning. So let’s look anew at PowerShell functions.
Let’s begin with some general guidelines.
A PowerShell function is a unit of code execution.
It is a single-purpose command.
Its behavior can be customized by parameters.
It writes one type of object to the pipeline.
At its core, a PowerShell function is nothing more than a named scriptblock. This makes it easier to invoke. The function’s code is generally the same code you would use interactively at a PowerShell prompt. Although, as you’ll learn, since we’re taking the time to commit the commands to a file, we often take advantage of PowerShell’s scripting language.