title: How to get the line number of error in PowerShell
body: <p>I'm trying to get the line number of an error when running a PowerShell script. Here is what I'm using at the moment:</p>
  
  <pre><code>$e = $_.Exception
  $line = $_.Exception.InvocationInfo.ScriptLineNumber
  $msg = $e.Message 
  
  Write-Host -ForegroundColor Red "caught exception: $e at $line"
  </code></pre>
  
  <p>Sometimes this works and sometimes it doesn't. I'm wondering if I'm doing anything wrong, or what I can do to make this work more consistently. </p>