title: Is there a way to enter the debugger on an error?
body: <p>Is there a way to enter the PowerShell debugger in response to an error? The <code>ErrorAction</code> parameter has several values but I don't see anything like <code>Debug</code>. What I would like is to open the debugger like it would open if I had set a breakpoint, but only when an error occurs (from, say, <code>Write-Error</code>).</p>
  
  <p><strong>Edit</strong>
  I should clarify a bit: I am primarily a C# developer and somewhat new to PowerShell, what I am expecting is behavior similar to what the Visual Studio debugger gives you for "unhandled exceptions". It appears that it is more common for PowerShell commands to throw exceptions, while custom scripts seem to mostly use Write-Error. I don't think I particularly care to distinguish between the two, but I would like to handle both. </p>
  
  <p>Trevor Sullivan's answer below mentions that you can use <code>Set-PSBreakpoint -Command Write-Error -Action { break; };</code> which appears to work well to catch those cases. Though, I am finding that in many cases it is actually a command throwing an exception that I would like to break on. Roman Kuzmin's answer appears to work if you set <code>$ErrorActionPreference = "stop"</code>, however, I have the problem that I cannot step through the program, it seems to break out of that location and end the script. If <code>$ErrorActionPreference = "continue"</code> it does not work for me. Traps in general seem to have a similar issue that they break out out of any nested scopes, which is not desired.</p>