47

In C#, I might do something like this:

System.Net.WebClient w = new System.Net.WebClient();
w.Credentials = new System.Net.NetworkCredential(username, auth, domain);
string webpage = w.DownloadString(url);

Is there a Powershell version of this, or should I just call through to the CLR?

Community
  • 1
  • 1
Ry Jones
  • 1,085
  • 1
  • 11
  • 25
  • In my enterprise, some guy dolike to annoy other with "usefull" preferences in IE. Is it possible to make http request without using IE? – MUY Belgium Jan 30 '19 at 09:43

3 Answers3

72

The PowerShell is almost exactly the same.

$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential($username, $password, $domain)
$webpage = $webclient.DownloadString($url)
John Rasch
  • 62,489
  • 19
  • 106
  • 139
Steven Murawski
  • 10,959
  • 41
  • 53
  • 2
    did this stop working for anyone else, all of the sudden? (401) Unauthorized – Northstrider Sep 21 '15 at 21:37
  • @meffect The comment below helped me. `$webclient.Credentials = New-Object System.Management.Automation.PSCredential($username, $password)` – T.CK Jan 26 '16 at 23:17
  • 6
    Note that if you just want to use the current logon credentials (e.g. to access an authenticated server on the intranet), you can use `$webclient.UseDefaultCredentials=$true` (as in [Ralph's answer](http://stackoverflow.com/a/12421075/1443496)). – Sean Allred Feb 12 '16 at 04:33
28

For those that need Powershell to return additional information like the Http StatusCode, here's an example. Included are the two most likely ways to pass in credentials.

Its a slightly modified version of this SO answer:
How to obtain numeric HTTP status codes in PowerShell

$req = [system.Net.WebRequest]::Create($url)
# method 1 $req.UseDefaultCredentials = $true
# method 2 $req.Credentials = New-Object System.Net.NetworkCredential($username, $pwd, $domain); 
try
{
    $res = $req.GetResponse()
}
catch [System.Net.WebException]
{
    $res = $_.Exception.Response
}

$int = [int]$res.StatusCode
$status = $res.StatusCode
return "$int $status"
Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
  • 2
    Excellent additional answer, this was actually exactly what I was looking for. – LimpingNinja Sep 16 '15 at 13:52
  • 6
    I may be wrong, but I had to replace `new NetworkCredential` with `$passwd = ConvertTo-SecureString "nowisthetime4U" -AsPlainText -Force;` followed by `$request.Credentials = New-Object System.Management.Automation.PSCredential ("pwatson_at_phs_org", $passwd);` – lit Sep 29 '15 at 16:53
  • 1
    New-Object System.Net.NetworkCredential instead of new NetworkCredential – Paul Martin Dec 05 '20 at 15:39
2

In some case NTLM authentication still won't work if given the correct credential.

There's a mechanism which will void NTLM auth within WebClient, see here for more information: System.Net.WebClient doesn't work with Windows Authentication

If you're trying above answer and it's still not working, follow the above link to add registry to make the domain whitelisted.

Post this here to save other's time ;)

Colin Su
  • 4,613
  • 2
  • 19
  • 19