Patrick Gruenauer has logging boilerplate code for us:
So you have already created your first PowrShell scripts? Now you want to enhance this scripts with error logging ? If your answer is yes, jump in this this article. I will show you how to implement a custom function that captures the errors and writes errors in an error log file. Let’s get started.
Click through for the code, as well as an explanation of each bit.