About

MS MVP Logo

View Marc Lognoul [MVP]'s profile on LinkedIn

Disclaimer

The information and materials in this site are provided "AS IS" with no warranties, and confering no rights. This site does not represent the thoughts, intentions, plans or strategies of our employers, customers, friends or family, solely our own personal opinions.

Uploading a file to a SharePoint document Library using PowerShell

by Marc 9. January 2009 21:05

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!

Tags:

PowerShell | SharePoint

Comments are closed