PS: Template Script


When writing PS Scripts, I find it helpful to come up with a template type system. Here is mine, feel free to modify for yourself.

UPDATE: 2017-12-23 = I no longer use this template script. I have since placed my scripts into modules and dot source them in the beginning. Also, instead of writing to the event log, I just write to a Logs folder within the module. This makes my modules more portable and self containing.


To Resolve:

2018-05 Update again, here is my current template script:

This uses the native Start-Transcript and gives the user a chance to remove logging if they wish. You may opt to use my older one that writes to log and event log.

1a. Update 2018-01-08: I now use the following template and Modules instead:


2. Copy and paste and paste your script’s main contents in the relevant sections:

2. Let me explain what the script does:

2a. It creates a file at c:\scripts\scriptname automatically for each script.

2b. It writes to the “Application” log in the Windows Event Viewer so it is easy to query. This is great as you will always be able to tell: if your script ran, if it completed, if it had errors, and it can output variables which change each time the script runs. You can easily integrate this in a log aggregation system.

2c. It logs statements like comments.

NOTE: Before using this template, please make sure to go over Begin, Process, and End in Powershell scripts.

NOTE: For details on how you can use this template to create scripts that run against multiple machines, check out my post on Running Scripts Against Multiple Computers