Walkthrough: An example of how I write PowerShell functions

A couple of days ago I posted a blog article titled "PowerShell function: Test-ConsoleColor provides a visual demonstration of the foreach scripting construct" and today I thought I would walk you through that function step by step since it's what I consider to be a well written PowerShell function.

It starts out by using the #Requires statement to require at least PowerShell version 3 or it won't run. It also requires that the PowerShell Community Extensions module be installed since it uses a function from that module and continuing without it only leads to errors:

#Requires -Version 3.0 -Modules Pscx

The function is then declared using a Pascal case name that uses an approved verb along with a singular noun. Comment based help is provided just inside the function declaration. This isn't the only location where comment based help can be specified at, but it's my preferred location for it.

Click here to be redirected to the original post of this article on the author’s blog site where you can read the remainder of the article.

µ

About Mike F Robbins

Mike F. Robbins is a Microsoft Cloud and Datacenter Management MVP for Windows PowerShell. He is the creator of The PowerShell Conference Book, author of PowerShell 101: The No-Nonsense Beginner’s Guide to PowerShell, co-author of Windows PowerShell TFM 4th Edition, and a contributing author of a chapter in the PowerShell Deep Dives book. Mike is also the leader and co-founder of the Mississippi PowerShell User Group. He blogs at mikefrobbins.com and can be found on twitter @mikefrobbins.