Skip to content

Instantly share code, notes, and snippets.

@9to5IT
Last active November 3, 2024 18:13
Show Gist options
  • Save 9to5IT/9620683 to your computer and use it in GitHub Desktop.
Save 9to5IT/9620683 to your computer and use it in GitHub Desktop.
PowerShell: Script Template
#requires -version 2
<#
.SYNOPSIS
<Overview of script>
.DESCRIPTION
<Brief description of script>
.PARAMETER <Parameter_Name>
<Brief description of parameter input required. Repeat this attribute if required>
.INPUTS
<Inputs if any, otherwise state None>
.OUTPUTS
<Outputs if any, otherwise state None - example: Log file stored in C:\Windows\Temp\<name>.log>
.NOTES
Version: 1.0
Author: <Name>
Creation Date: <Date>
Purpose/Change: Initial script development
.EXAMPLE
<Example goes here. Repeat this attribute for more than one example>
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
$ErrorActionPreference = "SilentlyContinue"
#Dot Source required Function Libraries
. "C:\Scripts\Functions\Logging_Functions.ps1"
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Script Version
$sScriptVersion = "1.0"
#Log File Info
$sLogPath = "C:\Windows\Temp"
$sLogName = "<script_name>.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
#-----------------------------------------------------------[Functions]------------------------------------------------------------
<#
Function <FunctionName>{
Param()
Begin{
Log-Write -LogPath $sLogFile -LineValue "<description of what is going on>..."
}
Process{
Try{
<code goes here>
}
Catch{
Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $True
Break
}
}
End{
If($?){
Log-Write -LogPath $sLogFile -LineValue "Completed Successfully."
Log-Write -LogPath $sLogFile -LineValue " "
}
}
}
#>
#-----------------------------------------------------------[Execution]------------------------------------------------------------
#Log-Start -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
#Script Execution goes here
#Log-Finish -LogPath $sLogFile
@klborders
Copy link

klborders commented Feb 17, 2019

This is a beautiful way of building out a script! TY!

Jsnover would be proud.

@krptodr
Copy link

krptodr commented Jan 10, 2020

@tresf

@99to5IT, my Get-Help won't work unless I put a newline between #requires -version 2 and <#. This seems to be consistent with PowerShell on Windows 10 as well as MacOS.

I can confirm, you are correct.

@dcvlehr
Copy link

dcvlehr commented Feb 13, 2020

Is this up to date for version 5? Or are there certain things that can be removed? Or replaced with a new v5 cmdlet or format?

@pvcodes-zz
Copy link

I only want a snippet that do

  1. when i enter "run"
  2. hit tab key, It simply autocomplete the expression to "g++ -g **.cpp -o main.exe && .\main.exe"

@MantvydasD
Copy link

What's the references to Log-Start, Log-Write, Log-Finish, Log-Error? There are no such cmdlets in Powershell. There are Start-Transcript and Stop-Transcript though.

@9to5IT
Copy link
Author

9to5IT commented Jun 20, 2022

Log-Start, Log-Write, Log-Finish and Log-Error are in reference to the PSLogging module which you can download from Powershell Gallery >>> https://www.powershellgallery.com/packages/PSLogging/2.5.2.

There is also more info here >>> https://9to5it.com/powershell-logging-v2-easily-create-log-files/

@hoppfrosch
Copy link

Why not getting the scriptname automatically?

$scriptName = $MyInvocation.MyCommand.Name
$sLogName = "$scriptName.log"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment