$webclient = New-Object System.Net.WebClient
$webclient.Headers.Add("user-agent", "Powershell webclient")
$webclient.Credentials = New-Object System.Net.NetworkCredential("usernam", "password", "domain")
$webclient.DownloadString("http://MySharePointServer.com")
However, this is the error I got:
Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (401) Unauthorized."
At line:1 char:26
+ $webclient.DownloadString <<<< ("http://MySharePointServer.com")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
I searched high and low but found nothing useful. Then out of the blue I thought I would try
$webclient.DownloadString("http://MySharePointServer.com/default.aspx")
and lo and behold, the page loaded. I guess there must be something going on where webclient does not handle the default document or something to do with how I setup my SharePoint site. I don't have time to dig deeper into the root cause, but problem solved, moving on.