SharePoint PowerShell How To Series – Foreword
I often find myself scratching around the local drive of my development machines for remnants of PowerShell scripts to generate list & libraries in SharePoint with all sorts of different folder hierarchies to test the performance of code I’m working on to ensure it scales well once the folder & item counts start getting up to high numbers. So rather than keep scratching I’m going to start posting my scripts as I create and use them so I can find/reuse them later on and someone else might find them useful as well. I’m sure they won’t work under all scenarios and situations (and they are not designed to). They are just quick scripts that serve a specific purpose to me at the time and you may be able to tweak to fit your needs.
Note this example doesn’t handle folders within folders. There are also several “no-code” methods for uploading a whole folder structure (and files) to SharePoint.
1. Create a folder containing the files to upload to SharePoint
2. Locate the SharePoint library that you want to upload the files to, take note of the:
- Web URL
- Document library URL
- Document library name
3. Copy the following script to be run in a PowerShell window
if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null) { Add-PSSnapin Microsoft.SharePoint.PowerShell } #Script settings $webUrl = "http://vs-server12" $docLibraryName = "Documents" $docLibraryUrlName = "Shared%20Documents" $localFolderPath = "C:\temp\Docs4SharePoint" #Open web and library $web = Get-SPWeb $webUrl $docLibrary = $web.Lists[$docLibraryName] $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() ForEach($file in $files) { #Open file $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead() #Add file $folder = $web.getfolder($docLibraryUrlName) write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..." $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true) write-host "Success" #Close file stream $fileStream.Close(); } #Dispose web $web.Dispose()
4. Change the PowerShell variables at the top of the script to match your SharePoint upload destination and the location of the folder of files to upload
5. Run the PowerShell script, you should see progress of the uploaded files as shown below
6. After the script completes you should see all of the imported files in your SharePoint document library
Related articles
- SharePoint PowerShell How To: Create SharePoint List Items (in Root and in Folders) for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Test Documents in Library Folders for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Document Sets for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Library Folders for Load/Performance Testing
- How to import (upload) an entire folder of files to SharePoint using PowerShell
Very helpful Cameron, I have to regularly uploads recorded training sessions as collateral for our end user support. This is a great way to expedite the process, so again, many thanks!
LikeLike
Thanks for your comments Gareth.
LikeLike
Use Long Path Tool for such problems, it works good…
LikeLike
Thanks so much. It helps me a lot.
Can I use the powershell script create folder? If I want to upload many folder and files.
LikeLike
Hi Ken,
I don’t have a script example that does exactly what you are after. But here’s another script of mine that shows you how to create folders in SharePoint:
https://camerondwyer.wordpress.com/2013/11/06/sharepoint-powershell-how-to-create-sharepoint-library-folders-for-loadperformance-testing/
Hopefully you can combine the two scripts yourself to get your solution. Good luck,
LikeLike
Cameron, I have just a quick question. Can you have your script to generate documents from a single document and then mass upload them to SharePoint Online? Like in your post here: https://camerondwyer.wordpress.com/2013/12/11/sharepoint-powershell-how-to-create-sharepoint-test-documents-in-library-folders-for-load-performance-testing/
LikeLike
Hi Robert,
The PowerShell cmdlets for SharePoint Online is very limited compared to the full SharePoint 2013 (or 2010) PowerShell cmdlets. It’s mainly limited to SHarePoint Administration tasks and not SharePoint content.
You can reference the SharePoint Client Side Object Model (CSOM) dll from PowerShell scripts to achieve what you are after – but you’re then really writing CSOM code (which is much nice doing from Visual Studio in a C# project). I haven’t done this from PowerShell directly (as I would do it straight from a C# project) but it should work fine. For examples of this technique please take a look at these links:
http://jeffreypaarhuis.com/2012/06/07/scripting-sharepoint-online-with-powershell-using-client-object-model/
http://www.sharepointnutsandbolts.com/2013/12/SharePoint-Online-and-MSOL-WAAD-PowerShell-Office365.html
LikeLike
Hi Cameron,
Do you also have code examples for setting document properties during the copy process?
Thanks,
Rich
LikeLike
Hi Rich,
Have a look at this article which shows a simple method to set properties on uploading a file:
http://social.technet.microsoft.com/Forums/office/en-US/ad6555c8-51ba-4c85-adcd-e2114fed2a4c/importing-metadata-to-document-library-columns
The above is a simplified example, it should work on text columns but other column types (e.g. Dates, Managed Metadata, Lookup, Hyperlink, Picture) will probably need to set set differently. If you are in this situation then maybe take a look at this article:
http://get-spscripts.com/2010/10/bulk-upload-files-with-metadata-into.html
Your version of SharePoint will also make a difference when you get into some of the column types, for example Managed Metadata columns are easier to set with SharePoint 2013 than it is in 2010.
LikeLike
Thanks for the info. Just remember that other tools exist as well. For example, you can use robocopy to mirror an entire file structure with just one command (and it handles differential changes easily)
LikeLike
Thanks for the tip Mark. I haven’t used robocopy personally but for those that want to take a look here’s a few links regarding copy to SharePoint using robocopy:
http://sharepoint.stackexchange.com/questions/72265/robocopy-to-copy-files-from-an-old-sp-to-the-new-one
http://www.infogenicsolutions.com/content/syncing-sharepoint-document-libraries-file-share
https://social.technet.microsoft.com/Forums/sharepoint/en-US/cc4e9915-08bc-4dd4-b508-ce387ed8aacd/copy-from-sharepoint-to-sharepoint-using-robocopy?forum=sharepointgeneralprevious
LikeLike
Also working in a similar way is DavCopy. For those looking at robocopy it might also be worth a look.
http://www.bluedoglimited.com/Downloads/SitePages/DavCopy.aspx
LikeLike
Hi Cameron
Quite straithforward article!
Many thanks for the share:)
Thanks again.
LikeLike
Thanks Cameron! This helped me a great deal!
LikeLike
You’re welcome, glad it helped.
LikeLike
good day, it’s useful script, thank you, but in my case it’s not working, could you please help? nothing happaned. and also could you explain about this field
$docLibraryUrlName = “Upload” ? In my case it looks like this:
https://sitename/funworld/psupp/gd/maga/Upload/Forms/AllItems.aspx
LikeLike
Hi Alex,
It’s hard to provide assistance in your scenario since it’s doing nothing. I’d suggest taking a look at this article to see if it gives you any ideas as to what might be failing
https://camerondwyer.wordpress.com/2014/06/18/getting-started-the-basics-of-using-powershell-with-sharepoint/
In your case the docLibraryUrlName should be the segment of the URL that is the document library. Your full URL contains the site collection (plus any subsites), then the library (which is the bit you need to assign to the variable, then the URL possibly also includes folders within the library and finally the Forms/AllItems.aspx is just static reference to show the view from the library. From looking at the URL I can’t tell which bit is site and which bit is library and which bit is folder structure within the library. If I had to take a guess I’d say try https://sitename/funworld/psupp/gd/maga as your site, then your docLibraryUrlName should be “Upload” or maybe your library is maga and you created a folder within it called Upload
LikeLike
Hi Cameron,
nice script. Works pretty well for me.
I tried to re-write to allow me uploading files from a folder given the filenames and Metadata from a CSV file. But my attempts failed. Do you have a solution here as well? I’m focused on Metadata edited within Excel and then uploaded to a SharePoint library.
LikeLike
Hi Wolfgang, thanks for the feedback glad you found the script useful. Unfortunately I don’t have any handy that will let you set all the different types of metadata columns, I’d suggest having a look at this resource as it sounds like its in the right area: https://spfileupload.codeplex.com/
Good luck.
LikeLike
Hi Cameron,
In our company a daily zip file gets regularly uploaded in sharepoint((For ex – 29-jan-2016-filename.zip) ,next day it would be 20-jan-2016-filename.zip.)
Is it possible to write powershell program that automatically downloads the daily file based on date to the loca machine ?
Thanks
Vishnu
LikeLike
Hi Vishnu. This is certainly possible using Powershell. I don’t have any scripts handy that do exactly what you are after, but take a look at this article for how to download a file from SharePoint (it provides a few different ways) http://techtrainingnotes.blogspot.com.au/2014/02/download-file-from-sharepoint-using.html
And this MSDN article provides lots of info on how to get the current date/time and format it in different ways in Powershell. Combine the two and you should be able to accomplish your goal.
https://technet.microsoft.com/en-us/library/ee692801.aspx
LikeLike
Hi Cameron, I’m trying to export a pdf from Tableau Server and import it directly to One Drive, i don’t want to store anything on local, any suggestion. Thanks Nish.
LikeLike
Sorry Nish I’ve never worked with Tableau. OneDrive has a REST API that might be useful for your purposes
LikeLike
Is there a way to run this script from Windows 10 PC?
LikeLike
Hi Cameron,
I have a opposite case. Can you provide something that copies all folders and files from SharePoint document library to file share maintaining folder/file structure with metadata? I am using https://sharepoint.stackexchange.com/questions/6511/download-and-delete-documents-using-powershell
It used to work but now all of a sudden it stopped working. It only deletes the folders but does not copy anything.
LikeLike
Actually now it is copying folder and files but not sub folders
LikeLike
I tried this script many times, this does not work and gives below error:
Exception calling “Add” with “3” argument(s): “0x80070003There is no file with URL
I think, if you have subfolders structure where you want to move the data below gives above error:
#Add file
$folder = $web.getfolder($docLibraryUrlName)
Do you have anything on this?
Thanks,
DC
LikeLiked by 1 person
Hi DC – this article shows how to upload to a folder within a document library rather than just to the root of a document library > https://camerondwyer.com/2013/12/11/sharepoint-powershell-how-to-create-sharepoint-test-documents-in-library-folders-for-load-performance-testing/
LikeLike