PowerShell Script Template
As of September 2015, I have released version 2 of my PowerShell Script Template, which includes a number of improvements over this version, in particular the use of the new PSLogging PowerShell module that significantly improves the creation and management of log files. This version of the template is no longer supported or maintained, so please upgrade to the new version today!
Version 2 of the template is available here – PowerShell Script Template v2.
Recently I put up an article on how to easily create log files in PowerShell. If you want to read that post, then check out this link – PowerShell: How to easily create log files for your scripts.
Today’s article is the next step in the process of creating awesome PowerShell scripts.
So far we have a standard PowerShell Logging Function Library that we can use in all our scripts to easily and quickly make some awesome looking log files. Now wouldn’t it be awesome if we have a standard PowerShell script template that we can fill out and use everytime we need to create a new script.
Not only that, but wouldn’t it be awesome if it already dot sourced the logging function and it also already had all of the LoG-Start and Log-Finish stuff in it, including version control and standard error handling that integrates with the Loggging Function we created earlier?
PowerShell Script Template: Quick Intro
The good news is…. I have already made a Powershell script template that includes all of this and more! It is really, really easy to use and self-exaplanatory… so all you need to do is fill in the gaps, remove the comment sections and then enjoy!
PowerShell Script Template: Why use it?
This template provides the following benefits:
- Easy to use;
- Self-Exlpanatory with heaps of comments;
- Standardises the look and feel of your scripts;
- Ensures easy readability of your scripts and comments;
- Provides your scripts will all the recommended meta information;
- Integrates with my PowerShell Logging Function Library for easy to use script logging;
- Provides a standard methodology for error handling;
- Makes your life much simpler!
PowerShell Script Template: The Template
Ok here it is (there will be a download link at the bottom of this page if your don’t want to copy this into your own .ps1 file)
PowerShell Script Template: Small things to note
Ok like I said the template is pretty much self-explanatory, however there are a few things I would like to point out:
- As mentioned previously, this template uses my PowerShell Logging Function Library. For more info click here to see the article.
$sScriptVersion– this is the variable that holds the current version of the script. This is written in the header of the log file
- Under the functions section, simply remove the
#>to uncomment the function. You can then copy this as many times as you want to create all your other functions in the script
- Uncomment each of the lines in the Execution section and of course replace the
Script execution goes hereline with each of the functions you want to call and in the order you want to call them
PowerShell Script Template: Download Link
If you don't want to copy the code above, then you can download the template from here: PowerShell Script Template
Do you need a PowerCLI Script Template?
If you are looking for a PowerCLI Script Template, then I have create one of those as well. You can get it from here >>> PowerCLI Script Template.
As per usual, if you get stuck on anything or are unsure about anything, please send me an email or better still use the comments below so that other people can benefit from it too. Also, if you have any new ideas or improvements let me know in the comments below.
I hope this helps some of you guys out and makes PowerShell scripting heaps more enjoyable.
Thanks everyone for the continued support.