title: How to differentiate Invoke-WebRequest exception (HTTP error code) in try catch statement
body: <p>I've created a script to download specific files on the Internet with a PowerShell script using the <code>Invoke-WebRequest</code> cmdlet.</p>
  
  <p>I've 2 known exception I would like to handle: 404 and 429.</p>
  
  <p>I'm currently working with a <code>try</code>/<code>catch</code> statement:</p>
  
  <pre><code>try {
      Invoke-WebRequest -Uri $download_url -OutFile $srr_file
  } catch {
      ...snip...
  }
  </code></pre>
  
  <p>But for both errors, I've the same code and it's not what I would like to do: </p>
  
  <pre><code>try {
      Invoke-WebRequest -Uri $download_url -OutFile $srr_file
  } catch [something here] {
      # Here the code I would like to write if the HTTP response is 404 File not found
      # Will certainly be a continue command
  } catch [something here] {
      # Here the code I would like to write if the HTTP response is 429 Too many request
      # Will certainly be a sleep command for 1 hour
  }
  </code></pre>
  
  <p>Here the output of the command in both situation:</p>
  
  <pre>
  Error HTTP 404
  Invoke-WebRequest : The remote server returned an error : (404) Not Found.
  At line:1 char:1
  + Invoke-WebRequest -Uri https://&lt;censored&gt; -Outfile test.txt
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation : (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
  
  Error HTTP 429
  Invoke-WebRequest : The remote server returned an error : (429) Too Many Requests.
  At line:1 char:1
  + Invoke-WebRequest -Uri "https://&lt;censored&gt; -Outfile test.txt
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation : (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
  </pre>