Script Template for PowerShell

<#
.SYNOPSIS
    Brief description of what the script does.

.DESCRIPTION
    Detailed description of the script's purpose and functionality.

.PARAMETER ParamName1
    Description of the first parameter.

.PARAMETER ParamName2
    Description of the second parameter.

.EXAMPLE
    Example-1: .\ScriptName.ps1 -ParamName1 Value1 -ParamName2 Value2
    Description of what this example does.

.EXAMPLE
    Example-2: .\ScriptName.ps1 -ParamName1 Value3
    Description of what this example does.

.NOTES
    File Name      : ScriptName.ps1
    Author         : Your Name
    Prerequisite   : PowerShell V3 or later
    Copyright      : (c) 2023 Your Company. All rights reserved.

.LINK
    Script posted over:
    http://www.your-website.com

#>

#Requires -Version 3.0
#Requires -Modules ActiveDirectory, Exchange
#Requires -RunAsAdministrator

[CmdletBinding()]
param (
    [Parameter(Mandatory=$true, 
               ValueFromPipeline=$true,
               ValueFromPipelineByPropertyName=$true, 
               ValueFromRemainingArguments=$false, 
               Position=0,
               HelpMessage="Enter the first parameter value.")]
    [ValidateNotNullOrEmpty()]
    [Alias("PN1")]
    [string]$ParamName1,

    [Parameter(Mandatory=$false)]
    [int]$ParamName2 = 0
)

Begin {
    # Initialize variables, import modules, define functions
    Set-StrictMode -Version Latest
    $ErrorActionPreference = "Stop"

    # Log file setup
    $LogFile = "C:\Logs\ScriptName_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
    
    function Write-Log {
        param([string]$Message)
        $LogMessage = "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): $Message"
        Add-Content -Path $LogFile -Value $LogMessage
        Write-Verbose $LogMessage
    }

    Write-Log "Script started"
}

Process {
    try {
        # Main script logic goes here
        Write-Log "Processing started"

        # Your code here

        Write-Log "Processing completed"
    }
    catch {
        Write-Log "An error occurred: $_"
        throw $_
    }
}

End {
    # Cleanup operations
    Write-Log "Script completed"
}

This template includes:

  1. A comprehensive comment-based help section at the beginning, which provides information about the script’s purpose, parameters, examples, and more.
  2. #Requires statements to specify prerequisites like PowerShell version, required modules, or administrator rights.
  3. [CmdletBinding()] attribute to make the script behave like a cmdlet.
  4. Parameter block with examples of mandatory and optional parameters, including parameter attributes.
  5. Begin, Process, and End blocks to structure the script’s execution.
  6. Error handling with try-catch blocks.
  7. Logging functionality to keep track of the script’s execution.
  8. Use of Set-StrictMode and $ErrorActionPreference for better error detection and handling.

You can customize this template based on your specific needs, adding or removing sections as necessary for your script.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *