title: Try-catch issue powershell
body: <pre><code>function Test($cmd)
  {
            Try
           {
                    Invoke-Expression $cmd -ErrorAction Stop
           }
           Catch
           {
                    Write-Host "Inside catch"
           }
  
           Write-Host "Outside catch"
  }
  
  $cmd = "vgc-monitors.exe"  #Invalid EXE
  Test $cmd
  
  $cmd = "Get-Content `"C:\Users\Administrator\Desktop\PS\lib\11.txt`""
  Test $cmd
  </code></pre>
  
  #
  
  <p>Call Test() first time with $cmd = "vgc-monitors.exe" (this exe does not exist in system)</p>
  
  <p>*Exception caught successfully</p>
  
  <p>*Text "Inside catch" "outside catch" printed</p>
  
  <h2>#</h2>
  
  <p>Call Test() second time with <code>$cmd = "Get-Content "C:\Users\Administrator\Desktop\PS\lib\11.txt""</code> (11.txt does not exist in the specified path)</p>
  
  <p>*Exception NOT caught</p>
  
  <p>*Text "Inside catch" not printed" </p>
  
  <p>*Get the following error message</p>
  
  <pre><code>Get-Content : Cannot find path 'C:\Users\Administrator\Desktop\PS\lib\11.txt' because it does not exist.
  At line:1 char:12
  + Get-Content &lt;&lt;&lt;&lt;  "C:\Users\Administrator\Desktop\PS\lib\11.txt"
      + CategoryInfo          : ObjectNotFound: (C:\Users\Admini...p\PS\lib\11.txt:String) [Get-Content], ItemNotFoundEx
     ception
      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
  </code></pre>
  
  <p>Question:</p>
  
  <p>I want the second exception to be caught. I cannot figure out the error in my code. </p>
  
  <p>Thanks</p>
  
  <p>Jugari</p>