Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PowerShell: Script Template Version 2 (with logging)
#requires -version 4
<Overview of script>
<Brief description of script>
.PARAMETER <Parameter_Name>
<Brief description of parameter input required. Repeat this attribute if required>
<Inputs if any, otherwise state None>
The script log file stored in C:\Windows\Temp\<name>.log
Version: 1.0
Author: <Name>
Creation Date: <Date>
Purpose/Change: Initial script development
<Example explanation goes here>
<Example goes here. Repeat this attribute for more than one example>
#---------------------------------------------------------[Script Parameters]------------------------------------------------------
Param (
#Script parameters go here
#Set Error Action to Silently Continue
$ErrorActionPreference = 'SilentlyContinue'
#Import Modules & Snap-ins
Import-Module PSLogging
#Script Version
$sScriptVersion = '1.0'
#Log File Info
$sLogPath = 'C:\Windows\Temp'
$sLogName = '<script_name>.log'
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
Function <FunctionName> {
Param ()
Begin {
Write-LogInfo -LogPath $sLogFile -Message '<description of what is going on>...'
Process {
Try {
<code goes here>
Catch {
Write-LogError -LogPath $sLogFile -Message $_.Exception -ExitGracefully
End {
If ($?) {
Write-LogInfo -LogPath $sLogFile -Message 'Completed Successfully.'
Write-LogInfo -LogPath $sLogFile -Message ' '
Start-Log -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
#Script Execution goes here
Stop-Log -LogPath $sLogFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment