Marc Lognoul's IT Infrastructure Blog

Cloudy with a Chance of On-Prem

SharePoint: Uploading a file to a document Library using PowerShell

Leave a comment

There are multiple methods to achieve this but since today it’s Friday and I’d like to watch Cloverfield’s making of before midnight :), here are two quick’n simple methods:

Method 1: Using WebDav and NET USE

Conditions: this method will work only if SharePoint is running on port 80, does not use form-based authentication and if the WebClient service is running on your client PC (or server, on which it is set to manual startup mode!)

Start PowerShell and enter the command

net use Z: “http://WSSSERVER/Site01/DocLibName/"

Then simply use the copy command:

copy c:demofile.txt Z:

Say what? Use PowerShell for that? It couldn't it work with a standard Windows command-prompt? Yes it can 🙂

Method 2: Using a standard HTTP “PUT”

Conditions: SharePoint should be configured to use form-based authentication, although the code could be adapted to support it.

Let’s use a function to tidy up the code…

Function UploadToSPDocLib($LocalPath,$spDocLibPath)

{

$WebMethod = "PUT"

$UploadFullPath = $spDocLibPath + $(split-path -leaf $LocalPath)

$WebClient = new-object System.Net.WebClient

$WebClient.Credentials = [System.Net.CredentialCache]::DefaultCredentials

$WebClient.UploadFile($UploadFullPath, $WebMethod, $LocalPath)

}

UploadToSPDocLib "c:demofile.txt" "http://WSSSERVER/Site01/DocLibName/"

While I still have other interesting ways to upload files to SharePoint, it will be the subject of another post…

And cut!

Advertisements

Author: Marc Lognoul

Relentless cloud professional. Restless rider. Happy husband. Proud father. Opinions are my own.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s