title: Get a line number where Write-Error occurred
body: <p>Consider the script</p>
  
  <pre><code>$someComplexCondition = $false
  
  if ($someComplexCondition)
  {
      Write-Error -Message "Some complex condition"
  }
  else
  {
      Write-Error -Message "Other complex condition"
  }
  </code></pre>
  
  <p>When I run it it says</p>
  
  <pre><code>C:\Dev&gt; .\MyScript.ps1
  C:\Dev\MyScript.ps1 : Other complex condition
  At line:1 char:15
  + .\MyScript.ps1 &lt;&lt;&lt;&lt;
      + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,MyScript.ps1
  </code></pre>
  
  <p>And I noticed that <strong>Write-Error</strong> always reports itself as <strong>at line:1</strong>
  According to the stack trace it looks like it's line #1 because it was called from the shell and more interesting <strong>char:15</strong> because</p>
  
  <pre><code>".\MyScript.ps1 ".Length -eq 15
  </code></pre>
  
  <p>If we change file name, this <strong>char:15</strong> will be changed accordingly.</p>
  
  <p>The question is how to get the actual line when the error occurred.</p>
  
  <p>In our case I would like to get <strong>line:9</strong></p>