title: Script to check the status of a URL
body: <p>I have seen a few questions similar to this but have not found one that will work for my situation.</p>
  
  <p>I have a list of URL's stored in a text file that I need to run through to see if they return a 404 error. I am using powershell and have been using the example here: <a href="http://gallery.technet.microsoft.com/scriptcenter/Powershell-Script-for-13a551b3#content" rel="nofollow">http://gallery.technet.microsoft.com/scriptcenter/Powershell-Script-for-13a551b3#content</a></p>
  
  <p>I am currently testing a link to a confluence page, watching the console in Chrome I can see the first status returned is a 404 - Not Found, then a dozen or so requests afterwards that are 304, 200. </p>
  
  <p>I am guessing the requests after the first 404 are affecting my results, I need the script to return based on that first response.</p>
  
  <p>I have tried powershell, php and javascript solutions with no luck so far.</p>
  
  <p>So all in all is there a way to return an answer based on the first response alone?</p>
  
  <p>The script:</p>
  
  <pre><code>## The URI list to test 
  $URLListFile = "H:\xxx\xxx\urlList.txt"  
  $URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue 
    $Result = @() 
  
  
    Foreach($Uri in $URLList) { 
    $time = try{ 
    $request = $null 
     ## Request the URI, and measure how long the response took. 
    $result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri } 
    $result1.TotalMilliseconds 
    }  
    catch 
    { 
     &lt;# If the request generated an exception (i.e.: 500 server 
     error or 404 not found), we can pull the status code from the 
     Exception.Response property #&gt; 
     $request = $_.Exception.Response 
     $time = -1 
    }   
    $result += [PSCustomObject] @{ 
    Time = Get-Date; 
    Uri = $uri; 
    StatusCode = [int] $request.StatusCode; 
    StatusDescription = $request.StatusDescription; 
    ResponseLength = $request.RawContentLength; 
    TimeTaken =  $time;  
    } 
  
  } 
      #Prepare email body in HTML format 
  if($result -ne $null) 
  { 
      $Outputreport = "&lt;HTML&gt;&lt;TITLE&gt;Website Availability Report&lt;/TITLE&gt;&lt;BODY background-color:peachpuff&gt;&lt;font color =""#99000"" face=""Microsoft Tai le""&gt;&lt;H2&gt; Website Availability Report &lt;/H2&gt;&lt;/font&gt;&lt;Table border=1 cellpadding=0 cellspacing=0&gt;&lt;TR bgcolor=gray align=center&gt;&lt;TD&gt;&lt;B&gt;URL&lt;/B&gt;&lt;/TD&gt;&lt;TD&gt;&lt;B&gt;StatusCode&lt;/B&gt;&lt;/TD&gt;&lt;TD&gt;&lt;B&gt;StatusDescription&lt;/B&gt;&lt;/TD&gt;&lt;TD&gt;&lt;B&gt;ResponseLength&lt;/B&gt;&lt;/TD&gt;&lt;TD&gt;&lt;B&gt;TimeTaken&lt;/B&gt;&lt;/TD&lt;/TR&gt;" 
      Foreach($Entry in $Result) 
      { 
          if($Entry.StatusCode -ne "200") 
          { 
              $Outputreport += "&lt;TR bgcolor=red&gt;" 
          } 
          else 
          { 
              $Outputreport += "&lt;TR&gt;" 
          } 
          $Outputreport += "&lt;TD&gt;$($Entry.uri)&lt;/TD&gt;&lt;TD align=center&gt;$($Entry.StatusCode)&lt;/TD&gt;&lt;TD align=center&gt;$($Entry.StatusDescription)&lt;/TD&gt;&lt;TD align=center&gt;$($Entry.ResponseLength)&lt;/TD&gt;&lt;TD align=center&gt;$($Entry.timetaken)&lt;/TD&gt;&lt;/TR&gt;" 
      } 
      $Outputreport += "&lt;/Table&gt;&lt;/BODY&gt;&lt;/HTML&gt;" 
  } 
  
  $Outputreport | out-file H:\xxx\xxx\test.htm 
  Invoke-Expression H:\xxx\xxx\test.htm   
  </code></pre>