title: How can I make Invoke-RestMethod use the default web proxy?
body: <p>The following works fine on my machine which does not use a web proxy.</p>
  
  <pre><code>return Invoke-RestMethod 
     -Uri $server$url 
     -ContentType $contentType 
     -Headers $headers 
     -Method $method 
     -UseDefaultCredentials 
  </code></pre>
  
  <p>Note: the <code>$server$url</code> is an <em>https</em> address, something like <em>https://somewhere.example.com/api/data</em></p>
  
  <p>Now I'm trying to get it to work in a corporate environment but I am getting a 401 error.</p>
  
  <p>I think this is because there is a corporate proxy which is defined with a proxy.pac file. I have confirmed I can get to the <code>$server$url</code> URI from Internet Explorer. What do I need to do to get the <code>Invoke-RestMethod</code> command to work using the same settings?</p>
  
  <p>I have tried adding the <code>-proxy</code> parameter</p>
  
  <pre><code>$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
  $proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
  
  return Invoke-RestMethod 
      -Uri $server$url 
      -ContentType $contentType 
      -Headers $headers 
      -Method $method 
      -UseDefaultCredentials 
      -Proxy $proxy 
      -ProxyUseDefaultCredentials
  </code></pre>
  
  <p>but the -Proxy parameter is expecting a URI not an <code>IWebProxy</code> object.</p>