Periodically upload files to SharePoint using Powershell

Periodically upload files to SharePoint using Powershell

You can use Powershell to upload files to a SharePoint document library. Let’s say some application is generating reports in C:\Reports and you want to periodically upload them to a document library Shared Documents. By sheduling a PowerShell script and uploading all files in a certain directory to SharePoint you can access of all your important reports via SharePoint.

The powershell script needed to upload files to SharePoint is quite easy:

$path = "C:\Reports";
$destination = "http://quahog/Shared%20Documents";

$securePasssword = ConvertTo-SecureString "secret" -AsPlainText -Force;
$credentials = New-Object System.Management.Automation.PSCredential ("QUAHOG\Administrator", $securePasssword);
#$credentials = [System.Net.CredentialCache]::DefaultCredentials;

$webclient = New-Object System.Net.WebClient;
$webclient.Credentials = $credentials;

Get-ChildItem $path | Where-Object {$_.Length -gt 0} | ForEach-Object { $webclient.UploadFile($destination + "/" + $_.Name, "PUT", $_.FullName); Remove-Item $_.FullName -Force };

This script will enumerate all files in C:\Reports larger than 0 bytes, upload them to the Shared Documents library and then delete those files. If you don’t want to specify your password in Powershell, delete line 4 and 5 and uncomment line 6 to use the current user credentials to upload the file.

You can schedule this script to run every night by using the Windows Task Scheduler. Start the Powershell console C:\Windows\System32\WindowsPowershell\V1.0\powershell.exe with an argument that points to the .ps1 script file.

Shedule Powershell Script

Voila, now you have configured yourself a script that will run every night and upload all reports from C:\Reports to your document library.

About the author


You can leave a response, or trackback from your own site.

11 Responses to “Periodically upload files to SharePoint using Powershell”

  1. ShawnC says:

    This is exactly what I was looking for. I couldn’t get it to work though. Not sure if it was due to permissions as I couldn’t figure out how to or where to put the password (newbie). I didn’t get any errors, nothing happened though.
    Any suggestions?


  2. Alain Alain says:

    @ShawnC: Substitute your real password for the phrase “secret” in ‘ConvertTo-SecureString “secret” -AsPlainText -Force;’

  3. ShawnC says:

    Thanks Alain,
    My biggest misstake was that my sample files were 0kb…. I know.. dah……..

  4. Alain Alain says:

    Good that you solved that problem

  5. Bill says:

    I’m getting an error:

    Exception calling “UploadFile” with “3” argument(s): “The remote server returned an error: (400) Bad Request.”
    At line:11 char:94
    + Get-ChildItem $path | Where-Object {$_.Length -gt 0} | ForEach-Object { $webclient.UploadFile <<<< ($destination + "/" + $_.Name, "PUT", $_.FullName); Remove-Item $_.FullName -Force };
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    But on the bright side, it deleted all of my files even though they weren't uploaded :( Probably should put some error checking in it.

  6. Mate says:

    Thanks Alain! In my case we had a SharePoint 2007 farm, and we need to upload some pics to SharePoint. WebClient is really the way to go.

  7. David says:

    Great piece of code but for my SharePoint site the files uploaded in a checked out state and I don’t have access to Microsoft.SharePoint.dll in order to check them in. Any help would be appreciated.

  8. Nico says:

    Thanks Alain for your Upload script.
    It’s function perfectly,but how can I do to not delete files in local folder(C:\Shared Folder)after it’s copy on sharepoint?

  9. Great, worked first time.

    Just need to work out how to handle subfolders now :)

  10. Patrick McKibbins says:


    I have a very similiar script to the one you published here. Mine works just fine uploading files. I’ve recently had the need to create a new folder via powershell prior to uploading the files.

    Do you have any suggestions on how to accomplish this?


  11. David Wallis says:

    So simple, yet effective! Took me longer to find the sharepoint url I needed than it did to integrate this into my script.

    Saved me at least a day, thanks :)

Leave a Reply