Powershell error handling: do something if NO error occured Powershell error handling: do something if NO error occured powershell powershell

Powershell error handling: do something if NO error occured


Check the automatic-variable $error after you cleared it.

$error.clear()try { something }catch { "Error occured" }if (!$error) { "No Error Occured" }


Another way:

$ErrorOccured = $falsetry {    $ErrorActionPreference = 'Stop'   ...something...}catch{   "Error occured"   $ErrorOccured=$true}if(!$ErrorOccured) {"No Error Occured"}

.


What about,

somethingIf ($?){    "No error"}Else{    "Error"}

This will work for non terminating errors too.