title: Piping credential into Invoke-WebRequest
body: <ul>
  <li>PowerShell 5.1.17763.592 / Windows Server 2019 Version 1809 (build 17763.737)</li>
  </ul>
  
  <p>I am trying to download a file with the <code>Invoke-WebRequest</code> cmdlet. The file is protected by HTTP basic auth. Doing a two-step like this works:</p>
  
  <pre><code>PS E:\&gt; $cred = Get-Credential
  
  cmdlet Get-Credential at command pipeline position 1
  Supply values for the following parameters:
  Credential
  PS E:\&gt; Invoke-WebRequest -Uri http://example.com/foo.zip -Credential $cred -OutFile $env:TEMP\foo.zip
  </code></pre>
  
  <p>Trying to pipe in the credential so I can do it in a one-liner fails:</p>
  
  <pre><code>PS E:\&gt; Get-Credential | Invoke-WebRequest -Uri http://example.com/foo.zip -Credential $_ -OutFile $env:TEMP\foo.zip
  
  cmdlet Get-Credential at command pipeline position 1
  Supply values for the following parameters:
  Credential
  Invoke-WebRequest : Cannot send a content-body with this verb-type.
  At line:1 char:18
  + ... redential | Invoke-WebRequest -Uri http://example.com/foo.z ...
  +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  + CategoryInfo          : NotSpecified: (:) [Invoke-WebRequest], ProtocolViolationException
  + FullyQualifiedErrorId : System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
  </code></pre>
  
  <p>I've googled the error but the only thing I can find is <a href="https://github.com/PowerShell/PowerShell/issues/2054" rel="nofollow noreferrer">Invoke-WebRequest using Get method doesn't allow content-body</a>, but if that's the root cause I don't understand why the two-liner would work. I suspect I've misunderstood how <code>$_</code> is evaluated.</p>