I’m always looking to improve my code while also keeping things as simple as possible. One thing that I find ever so annoying is having to figure out what a script does and determine if I can run it safely without some unwanted, irreversible side-effects. To that end, here’s a very simple script template that features two built-in levels of help.
Two parameters are provided in the template and you would of course want to add any additional parameters as needed.
-Help will produce exactly what a user would expect: information that identifies the script and its usage
-History is sort of an extended help with maintenance notes appended.
Use of either parameter will result in the display of information about the script and nothing more. Users of your scripts will always know that they can safely run your scripts with the -Help parameter to find out what it does and how to use it. Since -Help (or -History) will always cause the script to terminate early, there are no concerns about messing anything up.
One note, it is vitally important that the verbiage in your help information is accurate and makes sense. Obviously no one will find the built-in help very useful if it just contains what I’m providing in this template.
Not too much more to say about it as the script is fairly self explanatory.
Here’s a download link: template.ps1 (right-click and select save link as…)
(also listed below for cut-and-past or your online viewing pleasure)
Enjoy!
Param ([switch]$Help,[switch]$History) $HelpText = @' ####################################################################################### Name : Template.ps1 Date : 03/04/2011 Author : xb90@PoshTips.com Purpose : Describe the purpose here Usage : ./template [-Help][-History] where -Help produces help output -History produces help output with maintenance history Notes : Include any additional notes here if needed (otherwise delete this section) ####################################################################################### '@ $HistoryText = @' Maintenance Log Date By Updates (insert newest updates at top) ---------- ---- --------------------------------------------------------------------- 03/04/2011 xb90 New Script ####################################################################################### '@ if ($help -or $History){ $HelpText; if ($History){$HistoryText} exit } ## ## Main Script ## function FancyDisplay{ param ([string]$txt,[int]$len) write-host $txt.padright($len) -back cyan -fore blaCK } FancyDisplay "" 40 FancyDisplay " PowerShell Script Template" 40 FancyDisplay " Courtesy of PoshTips.com" 40 FancyDisplay "" 40 |
It would be totally awesome if you would put a download button around here somewhere. Sadly, i’m an idiot with computer code and I have no idea if we are supposed to use the code above to somehow use your template. If that’s the case, then a quick instructional on how to use the coding to run your program would be awesomely helpful for a sap like me and probably a lot of other people.
I found this site by googling “simple script template.” I don’t know if you really care how I found the site, but just in case.
Hey Mike,
I understand the frustration with the lack of any easy-to-use download button and I do appreciate knowing what google search landed you here, Thanks!
At one time the plugin that I use on this site to display the syntax-colorized powershell code also allowed had a download feature but unfortunately that seems to have been broken a while back after another necessary update was applied to the site. I’ve not yet found another plugin to replace the broken one so I started adding my own download links on some more recent posts.
I’ve added a download link to the page (just above the code listing).
For now, the easy way to get the code off any other pages that haven’t yet been updated would be to click the “VIEW CODE” link at the top of the code listing. That should open a new browser window that will contain only the code shown in the script. Next, you can copy the code out of the new browser page (Ctrl-A to select all, then Ctrl-C to copy) then open a text editor and Ctrl-V to paste it all into the text editor. All that is left is to save the file and then run it in Powershell.
Hope this Helps and thanks for visiting PoshTips.com!