How to catch error from sql server on powershell?
Powershell supports try/catch/finally. This allows you detect and handle errors within the scope of try block. 

If you simply want to stop the PS script if any errors occur, including non-SQL errors, wrap the main code in a try block:

try {
    runScriptsFromFolder -pathToFolder "C:\SqlScripts"
}
catch {
    throw
}