There are a lot of best practices when it comes to PowerShell scripting. This is the template I like to use.
Some of the features of the template are:
- Command based help. The synopsis header helps us and others to make sense of the script’s purpose. The help can be displayed by using
get-help
, described here. - A
requires
statement, to make the script work for PowerShell versions later thanv3.0
. We can also easily require admin access or a specific module to be available. Set-StrictMode
to enforce coding rules.- Set the error action preference to stop. When something doesn’t behave properly we want the script to fail fast, with a lot of noise.
- A transcript in the temp folder with a name based on the script name and the DateTime when it was run.
- A stopwatch to record the time taken.
<#
.SYNOPSIS
Script to <what will the script do>
.DESCRIPTION
This script will <Elaborate on what the script does>
.PARAMETER Param1
Specifies <What? Is the parameter required?>
.INPUTS
<Does the script accept an input>
.OUTPUTS
A log file in the temp directory of the user running the script
.NOTES
Version: 0.1
Author: Sven de Windt
Creation Date: <Date>
Purpose/Change: Initial script development
.EXAMPLE
<Give multiple examples of the script if possible>
#>
#requires -version 3.0
#-----------------------------------------------------------[Parameters]-----------------------------------------------------------
param(
[CmdletBinding()]
[parameter(mandatory = $false)][String]$Param1
)
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
Set-StrictMode -Version Latest
# Set Error Action to Silently Continue
$ErrorActionPreference = "Stop"
# Dot Source required Function Libraries
#. "C:\Scripts\Functions\Logging_Functions.ps1"
#----------------------------------------------------------[Declarations]----------------------------------------------------------
$LogNumber = Get-Date -UFormat "%Y-%m-%d@%H-%M-%S"
$Log = "$($env:TEMP)\$($MyInvocation.MyCommand.Name) $($LogNumber).log"
$ScriptVersion = "0.1"
#-----------------------------------------------------------[Functions]------------------------------------------------------------
#-----------------------------------------------------------[Execution]------------------------------------------------------------
Start-Transcript -Path $Log -NoClobber
$StopWatch = New-Object System.Diagnostics.Stopwatch
$StopWatch.Start()
Write-Output "Start script - version $($ScriptVersion)"
#-----------------------------------------------------------[Finish up]------------------------------------------------------------
Write-Output $StopWatch.Elapsed
$StopWatch.Stop()
Write-Output "Finished script - $($MyInvocation.MyCommand.Name)"
Stop-Transcript
Enjoy!