Navigate Up
Sign In

Using Powershell to... upload documents in SharePoint 2010

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.


Editor's note: Contributor Craig Pilkenton is a Senior Microsoft Consultant for

Powershell is the tool for getting things done in all versions of SharePoint (and your servers/desktops too!). It has the capability to automate, monitor, notify, and even 'react' to results. Not only does it have custom compiled 'activities' to get work done, it has the ability to call any command-line executable or pull in .NET library's to handle anything not already given to us by Microsoft (or even our own cmdlets). In this article we'll go over how to use some of the new "manage content" SharePoint cmdlets to interact with the platform just as a user would.

SharePoint 2010 has over 600 built-in cmdlets (pronounced as command-lets) to build, manage, and update the platform (see the TechNet reference). If you've ever opened a DOS command prompt, typing “dir” or “cd” to get a list of files, then you are ready to use Powershell. If not, Ravikanth C.'s article ' Getting started with PowerShell for SharePoint 2010 Administrators' is a great primer for this.

Now we move onto the setup of building a reusable, parameterized script file (.ps1) to do our work. While we have the ability to build our own cmdlets using Visual Studio 2010, using .ps1 files allows us to modify the code as often as need with it only being compiled at runtime by Powershell. Once the logic and functionality is rock-solid you could then convert it into your own Verb-Noun cmdlet.

There are a couple of considerations as you get started:

  • Security: the account that will be running as for the Powershell script needs to have at least Contributor rights on the SharePoint site you are targeting, as well as read on the file location. If you plan on using this as a Scheduled Task, you may want to run as the Farm Administrator account so there are no permission issues.
  • Toolset: while you can just use Notepad on your computer/server to put this together, there are many free tools out there that will give you IntelliSense, color-coding, debugging, and even version control if needed. My favorites are PowerGUI, PowerSE, NotePad++, and of course the Windows ISE (Server 2008 & Windows 7).

Let's get to work. Here's the file we'll be working from in this example:


I've marked up the file to denote what each section does but I'll call out the important areas below by line number.

  • Line 1: this is an example for calling the file with parameters and their format. When saving, name the file in a format that makes sense to you.
  • Lines 2-7: receive passed-in parameters make this script reusable so it can be called by other master scripts and/or be called as a Scheduled Task--
    • SharePoint site URL
    • The Document Library on the above site to target for injection (can also target a Folder/SubFolder in the Library)
    • The file name and its network location (e.g. ""\\server\some_directory\", etc.)
  • Lines 20-22: this section uses the new Get-SPWeb cmdlet and its methods for injection--
    • Store a reference to the site in the $spWeb variable
    • Store a reference to a Document Library in the site in the $spFolder variable
    • Store a reference to the FileCollection object inside the Document Library in the $spFileCollection object
  • Lines 25-26: here we store a reference to the file in the $file variable for use later and checks it length to verify it exists
  • Lines 29-30: in this portion using the FileCollection variable created above, we get a reference to the file in the Library (if it exists) and force a Check-Out--
    • Note: if it doesn't already exist an error will show on the .CheckOut() command but the script will continue on
  • Line 33: now to inject the file we call the add file method for the FileCollection variable giving it the file name to add as (we could make it something else) and the file variable using its .OpenRead() method
  • Lines 36-39: in this final section we finish up the processing--
    • Rebuild the FileCollection variable (otherwise our new file won't show if it didn't exist before)
    • Obtain a reference to the file in the Library again
    • Fire the the Check-In command for that file
    • Dispose of the reference to the Site from the Get-SPWeb cmdlet (Note: this is very important to avoid memory issues with unclosed SharePoint references

Now that we have the basics put together, to get it to do the work we need open a Powershell command prompt & navigate to the directory where your scripts will live (e.g. "CD E:\Powershell\" + Enter, etc.).


Once there we type the name of the script we saved and add in the parameters setup initially to start running the script:
.\SharePoint_FileCollection_Submit.ps1 "" "Reports" "\\A01\SharePoint\files" "Something.PDF"


The Write-Host sections I added will tell us the progress of our script, including any errors that may occur.

And that's all we need to post files to SharePoint. We could also enhance the file's capabilities such as setting Metadata values on that document, adding Check-In comments, enumerating through multiple files in a directory instead of just one, or even firing other Powershell scripts letting user's know the work was completed. If you'd like to utilize a copy of the code for yourself, here is a link to my SkyDrive location, the file is called UploadDocsToSharePoint.ps1.

Categories: PowerShell; SharePoint; MOSS; WSS; 2010


Peter van den Berg

Multiple files

Hi Graig, great post! I really like this. I do have one question, though. Now you have to set every single file "do something.pdf". How can I change the script that I can upload a batch file of documents that are in the folder location? I want to make a reference to the folder and that it copies every file. So something like "*.jpg" 

Posted 16-Feb-2012 by Peter van den Berg
Pilkenton, Craig

RE: Multiple files

Hi Peter, glad you like the article. One of the way I've used this script is to have a 'master' script that loops through a directory & calls this one as it loops. But here's also how to change this one to do multiple files in a directory: 1) on Line 25 change the variable to '$rqurdstrFileLocation' to get ahold of the directory itself; 2) then wrap Lines 29-38 in a "foreach ($indfile in $file | Where {$_.Name -like "*.jpg"}) { ...}" This will loop through all child objects and fire the commands just the same. Hope that helps.

Posted 16-Feb-2012 by Pilkenton, Craig


Great info Peter. My company has numerous reports that come in from all over the world in emails and Excel. We are trying to figure out how to configure a ShatePoint 2010 site running Performance Point Services to auto upload the newest version of the reports when they come in so the spreadsheets display continuous updated data. Can this be done using PowerShell?

Posted 30-Aug-2012 by Steve
Pilkenton, Craig

RE: Reporting

Thanks for commenting Steve.  Yes, Powershell can pick up files from any UNC path, as I did, or pull from a web service (e.g. SharePoint Document Library's web service) as mentioned on  For the 'continuously updated' data, when you upload the file(s) you just have to make sure the file name is the same if you want to overwrite in SharePoint and let Version History function, or you could put it in a Document Library with date-inserted file name with the view sorting by date descending.  Hope that helps.

Posted 30-Aug-2012 by Pilkenton, Craig

SP 2007

Can we do the same for Sharepoint 2007?

Posted 13-Sep-2012 by Game
Ken Barnes

Nice Article Craig

Have you had the opportunity of checking when documents/files need to have various automated content types attached to various documents

Posted 19-Oct-2012 by Ken Barnes
Alan Smith

Migrate metadata?

You mention setting metadata for the items. Is it possible to set the "Modified by" tag to the owner or last editor of the file from the file system? Something like what (Get-Acl C:\test.txt | select owner) returns?

Posted 23-Oct-2012 by Alan Smith

Cross Domain

Hi Craig,
That was a great article. Just wanted to know if it is possible to upload documents from a fileshare that is situated in a diffrent domain to SharePoint that.

Posted 25-Jul-2013 by Ashis

File download

I wasn't able to find your script at the skydrive link you provided.

Posted 06-Sep-2013 by Ryan
Cee Koz

File Download

I also was not able to download the PS1 script

Posted 04-Jan-2014 by Cee Koz

Nice and Useful Info

Hi you have provided a structured info in a very nice manner. Keep up the good work

Posted 10-Jan-2014 by kamal

Notify me of comments to this article


Add Comment