Powershell script template

To make live a bit easier when writing powershell scripts, it is a good idea to have some kind of a script template.

Why ?

By using a template you don’t have to worry about the standard stuff you always put in a script. And you can also have some customs functions already available. It is a good starting point for writing the script you want.
And it helps you to standardize you scripting. Which makes it in the end easier to maintain etc…
Using a template doesn’t have only pro’s but also con’s. If you just want to write some litte scriplet… but you are ‘forced’ to use a template, it will cost you a bit more time. And you are not that flexible in writing code, because when using a template, it is best to write in the filosofi of the the template.

When you also use GIT to maintain your code. You could start a new script by forking a new repository from your template script repository.

What is a good template ?

Yeah… good question…
Foremost, it should answer your needs and culture of writing code. If not, then or change your culture of writing or re-define your template.

Define areas for different stages of your script. I use, for instance, the begin{},process{},end{} codeblocks to structure the script.
In the begin{} codeblock, I’ll have my custom defined functions, some default parameters that I use,Input parameters,  etc..
In the process{} block I’ll put the major part of the code. You could say that it is the main part from where functions are called.
The end{} block contains some instructions to clean-up.

custom functions

I’ve created some custom functions that I use often. Functions to load PowerCLI, exit the script always in the same way, log functions (text and to syslog servers).
The exit-script function is one I would recommend to use. It is the last script routine you call before ending the script. But also when you have some kind of error handling. You will always end the script in the same way.


See https://github.com/brtlvrs/posh-template for an example of a powershell template.
This template contains the following areas:

  • comment section according to PowerShell definitions
  • param() section
  • begin{} scriptblock
    • script initialization code like:
      • determining location of script
      • loading parameter file (containing script parameters / variables)
      • inittialisation of log file (if needed)
    • region for default functions like
      • import-powercli
      • exit-script
    • region for script specific functions
  • process{} scriptblock
    The area containing the specific code for the purpose of the script
  • end{} scriptblock

Leave a comment or on the github repository, or underneath if you have questions, suggestions etc…
thank you for reading this post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s