PowerShell script template

This file is the Powershell template file that I use when creating PowerShell scripts.

The code below is a functioning PowerShell script for you to build on, it has the syntax for the Get-Help cmdlet to function correctly and takes three parameters some mandatory some not, it also outputs to the screen the script execution start & end time along with elapsed time.

Download file : Powershell Template as a text file.

  1. <#
  2. .SYNOPSIS
  3. This script will ....
  4.  
  5. .DESCRIPTION
  6. A Script description & version history.
  7.  
  8.  
  9. --------------------------------------------------------
  10. History
  11. Version number | Data | Comment
  12. 1.00 | <date> | Initial script.
  13. --------------------------------------------------------
  14.  
  15. .LINK
  16. http://www.glossover.co.uk/archives/322
  17.  
  18. .EXAMPLE
  19. <scriptName> -Param1 value -Param2 value
  20. <scriptName> -Param1 value -Param2 value -Param3 (optional) value
  21.  
  22. #>
  23.  
  24.  
  25. Param (
  26. [Parameter(Mandatory=$true, HelpMessage="Please enter a string value")]
  27. [alias("Param1")]
  28. [ValidateNotNull()]
  29. #String value.
  30. [string]$stringValue,
  31.  
  32. [Parameter(Mandatory=$true, HelpMessage="Please enter a numeric value.")]
  33. [alias("Param2")]
  34. [ValidateNotNull()]
  35. #Number value.
  36. [int]$intValue,
  37.  
  38. [Parameter(Mandatory=$false, HelpMessage="Please enter a string value (Optional).")]
  39. [alias("Param3")]
  40. [ValidateNotNull()]
  41. #Optional string value.
  42. [string]$optionalStringValue
  43.  
  44. )
  45.  
  46. $startTime = Get-Date
  47. Write-Host "Start time : $startTime"
  48.  
  49.  
  50. #Passed values
  51. Write-Host "Param1 : " $stringValue
  52. Write-Host "Param2 : " $intValue
  53. Write-Host "Param3 (optional) : " $optionalStringValue
  54.  
  55.  
  56. $endTime = Get-Date
  57. Write-Host "Script execution time : $( New-TimeSpan $startTime $endTime)."
  58. Write-Host "End time : $endTime"