Authenticating to a remote server using "UseDefaultCredentials"

Hey everyone,

I’m currently working on a script that uploads a file to a remote sharepoint server - the script below is what is currently being used:

$UploadFile = Get-ChildItem $CSV_File

# put the file up on SharePoint
$webclient = New-Object System.Net.WebClient

# set credentials to use
$securePassword = ConvertTo-SecureString '' -AsPlainText -force

$credentials = new-object System.Management.Automation.PSCredential ("Domain\Account", $securePassword)

# upload file
$webclient.UploadFile($destination + "/data.csv", "PUT", $Data.csv.FullName)

This method works just fine and uploads the resource to the server. However, I need to authenticate/upload to the remote server without storing the account credentials anywhere on the box. I’ve done this in the past by utilizing the “UseDefaultCredentials” property:

$webclient.UseDefaultCredentials = $true

However, even when switched to True, the credentials aren’t stored and authentication fails. Is there any reason in particular it wouldn’t be storing the credentials properly, and can another method be implemented to avoid storing credentials on the server?


How are you executing the script? Interactively, from a scheduled task, through PSRemoting, etc?

Hey Dave,

Presently, I’m testing it interactively (Running directly on the box) - however, this script is going to be moved over/rolled into to scheduled tasks on the server once it’s fixed.

Does the website you’re uploading to have Windows Authentication enabled? That’s required for UseDefaultCredentials to work.