title: Use PowerShell to automate website login and file download
body: <p>I want to use PowerShell to automate logging in to a website and downloading a PDF file. There are loads of examples on the internet that show how to do this (using <code>Invoke-WebRequest</code>, <code>WebClient</code>, <code>HttpWebRequest</code>, or <code>InternetExplorer.Application</code>), but most don't require a login first. Some show it with login, but I can't get them to work. I'm close with using <code>InternetExplorer.Application</code>:</p>
  
  <pre><code>$username = "xxxxx"
  $password = "yyyyy"
  $url = "https://example.com/login.aspx"
  $usernameElementId = "aaaaa"
  $passwordElementId = "bbbbb"
  $submitButtonElementId = "ccccc"
  
  $ie = New-Object -com InternetExplorer.Application
  $ie.Visible = $true
  $ie.Navigate($url)
  
  while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
  
  $ie.Document.getElementById($usernameControlId).value = $username
  $ie.Document.getElementById($passwordControlId).value = $password
  $ie.Document.getElementById($submitButtonElementId).click()
  
  while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
  Start-Sleep -m 2000
  
  $url = "https://example.com/statements/201607.pdf"
  $outFilePath = "C:\Downloads\Statement_201607.pdf"
  $ie.Navigate($url)
  
  while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
  
  # Script works up to this point--the pdf document is shown in IE.
  #The file downloaded in the next step is empty.
  
  $ie.Document.body | Out-File -FilePath $outFilePath
  </code></pre>
  
  <p>My question: how do I get the PDF document downloaded in the last step of the script?</p>
  
  <p>I've tried to do this same task with <code>WebClient</code> and <code>Invoke-WebRequest</code>, but I keep getting errors, because of the authentication piece. I've tried capturing the cookies after login and passing them with the next request, but nothing. If someone has a working example of doing this using another means, I'm all ears. In fact my preference would be to avoid automating IE, if possible, but I'll take any working solution.</p>