This is the PowerShell Template I use for 90% of my code:
PowerShell uses #region and #endregion to tell PowerShell editors that this is a section of code that can be collapsed. PowerShell-Template.ps1 includes a region around the whole Comment block, Parameter block and Function block so that the whole template can be collapsed with one click to show the main entry point as shown:
There are additional regions defined within the header. For more information:
PowerShell comment blocks for scripts and functions are well defined and are used for the help command.
In PowerShell 5 there are 14 valid comment-based help keywords as defined here:
I only use the following keywords in my template
The Functions Configure-Logs and Export-Logs are used together to create standard and error log files without the need to customise them per script.
Configure-Logs creates files path in the same location as the script with the date appended and starts transcription logging is the -logging switch is used.
If the script was called:
then the two log paths created would be
The path is tested to ensure it is writable, if it is not as in the case of a CD-ROM then a fall back path of $env:LOCALAPPDATA\temp\ is used:
Export-Logs is the last command run in the template. It closes transcription logging if enabled and if standard error ($error in PowerShell) is non zero then writes the error content out to the error log file. Instead of simply piping $error to a file, Export-Logs loops through each error in order and extracts the full string of:
Fully Qualified ID
Script Stack Trace and
The script line number that the error was generated on.
The final function included in the template is the Windows form function as discussed here: