Hey there!
I have recently played around with an error handling, also using functions.
You can explicitly define the behaviour of the script and how it should handle errors using an $ErrorActionPreference variable. You will typically set it to either of following: Continue reading Error handling in Powershell