My PowerShell exceptions aren't being caught
When you use the Stop action PowerShell changes the type of the exception to:
[System.Management.Automation.ActionPreferenceStopException]
So this is the what you should catch. You can also leave it out and catch all types.Give this a try if you want to opearte on different excdeptions:
try { Remove-Item C:\pagefile.sys -ErrorAction Stop}catch{ $e = $_.Exception.GetType().Name if($e -eq 'ItemNotFoundException' {...} if($e -eq 'IOException' {...}}
I like the Trap method for global error handling, as I wanted to stop my scripts if unhandled exceptions were occurring to avoid corrupting my data. I set ErrorActionPreference to stop across the board. I then use Try/Catch in places where I may expect to see errors in order to stop them halting the scripts. See my question here Send an email if a PowerShell script gets any errors at all and terminate the script