PowerShell Template–Because we all want some sort of consistency!

Does your company have a lot of PowerShell scripts? Do they all follow the same template? No? You are not alone.

In my experience, troubleshooting scripts is much easier if all the scripts have the same look.

This script not only uses logging for errors, but can also be run in verbose mode so that the errors display in the PS session.

How to use the PowerShell Template

Download the template:
Script Information

Edit the script name, description, and author.

Hey, if you are super cool you could add help features. I’m not that cool.

You might even want to add a changelog!

Function Main

This is the function that runs the entire script.

The build in functions are:

· LogTraceMessage

o This function logs all of the activity from the script into a log file

· SetGlobalEnvVariables

o This is where ALL script variables go. You don’t want to have to change variables throughout the script. Instead, you want a central location for them

· ImportPSModules

o This is where you would import any PS modules needed for the script

· Add your own functions

Function SetGlobalEnvVariables

This is the function that sets all of the variables used in the script.

The $Script.LoggingPath variable is where the log file will get written to.

Function ImportPSModules

This is the function that imports PowerShell Modules. I usually create a variable for the module in the SetGlobalEnvVariables function and then reference them here.

Function LogTraceMessage

This is the function that will construct the log file containing any errors.