title: Download file in powershell script
body: <p>I'm trying to find a method of downloading a file in a powershell script. From the website side I can see the URL as:</p>
  
  <pre><code>https://website.com/admin/listCC.cfm?reportCode=105&amp;clientId=11111&amp;beginDate=04/01/2016 00:00&amp;endDate=04/01/2016 23:59&amp;download
  </code></pre>
  
  <p>Clicking the link in IE would give a save, save as, or open prompt and it is a TXT file. I've tried using webclient code as well invoke-webrequest but the output is always like this:</p>
  
  <pre><code>    &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt; Session Timed Out&lt;/title&gt;
  
      &lt;SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"&gt;
      &lt;!--
      function sessionDone(){
          alert('Your session has expired.  Please login again.');
          window.location = "/admin/logout.cfm";
          }
      //--&gt;
      &lt;/SCRIPT&gt;
  
  
      &lt;meta http-equiv="Pragma" content="no-cache" /&gt;
      &lt;meta http-equiv="cache-control" content="no-store" /&gt; 
      &lt;meta http-equiv="Expires" content="-1" /&gt;
  
      &lt;/head&gt;
      &lt;body onLoad="sessionDone();"&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>Any help would be greatly appreciated</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I figured it out. I must have not been logging in properly with the initial invoke-webrequest code i was using. Here is what i'm using now</p>
  
  <pre><code>$yesterday = (Get-Date).AddDays(-1).ToString('MM/dd/yyyy')
  $loginsession=$null
  $loginform=$null
  $loginpage=$null
  $login=$null
  $dl_105=$null
  
  
  $loginpage = Invoke-WebRequest ("https://website.com/loginpage") -SessionVariable loginsession
  $loginform=$loginpage.Forms[0]
  $loginform.fields["username"] = "username"
  $loginform.fields["password"] = "password"
  
  $login = Invoke-WebRequest -Uri ("https://website.com/" + $loginform.Action) -WebSession $loginsession -Method POST -Body $loginform.Fields
  
  
  $dl_105=Invoke-WebRequest -Uri ("https://website.com/admin/listCC.cfm?reportCode=105&amp;clientId=11111&amp;beginDate=$yesterday 00:00&amp;endDate=$yesterday 23:59&amp;download") -Method GET -ContentType "text/csv" -Body $loginform.Fields -WebSession $loginsession
  Out-File -FilePath C:\Users\Administrator\Desktop\test\new_test.txt -InputObject $dl_105.Content -encoding ASCII
  
  sleep 5
  </code></pre>
  
  <p>I was lucky that the download link I was trying to get to wasn't entirely unique. All I'm doing is passing in yesterday's date to get the file i needed.</p>