Use PowerShell Cmdlets to Manage SharePoint Document Libraries

 

Summary: Management of SharePoint 2010 document libraries by using Windows PowerShell cmdlets is explored in this Hey, Scripting Guy! Blog post.

 

Hey, Scripting Guy! Question Hey, Scripting Guy! Please tell me about document management with SharePoint and Windows PowerShell.

— MK

 

Hey, Scripting Guy! Answer Hello MK,

Microsoft Scripting Guy Ed Wilson here. It is Thursday in Charlotte, North Carolina, and our last day with guest blogger Niklas Goude, who has been sharing his expertise with SharePoint and Windows PowerShell all week.

Niklas Goude is a Windows PowerShell MVP working at Enfo Zipper in Stockholm, Sweden. Niklas has extensive experience in automating and implementing SharePoint environments using Windows PowerShell. He has written a Windows PowerShell book for Swedish IT pros, http://powershell.se, and is currently co-authoring a book with Mattias Karlsson titled, PowerShell for Microsoft SharePoint 2010 Administrators, which will be published in English by McGraw-Hill in October 2010. Parts of this post are taken from Chapter 16 of that book.

Niklas also runs the blog, http://powershell.nu, where he shares scripts, examples, and solutions for administrative tasks in Windows environments through Windows PowerShell.

 

Creating Document Libraries

Working with document libraries is similar to working with SharePoint lists, as described in yesterday’s post. In this post we will create a new document library and see examples of how to upload documents to a document library.

Creating a new document library using Windows PowerShell is very similar to the creation of any other type of list. We can use the same Add() method provided by the SPListCollection class. In the example below, we use the Get-SPWeb cmdlet to retrieve a specific site, store a TemplateType in a variable, and then use the Add() method to create a new document library:

PS > $spWeb = Get-SPWeb -Identity http://SPServer
PS > $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
PS > $spWeb.Lists.Add(“My Documents”,“My Doc Library”,$listTemplate)

Modifying a Document Library

We can retrieve an existing document library using the GetList() method, just as we did with lists. The only difference is the relative URL used with the method:

PS > $spDocumentLibrary = $spWeb.GetList(“My Documents”)

Next, we can modify the properties of a document library. If we want to change the Description, we can simply type:

PS > $spDocumentLibrary.Description = “Lots of Documents”

Adding Document Library to Quick Launch is also a simple task when using Windows PowerShell:

PS > $spDocumentLibrary.OnQuickLaunch = “True”

When we’re are done with the updates we use the Update() method to commit the changes.

PS > $spDocumentLibrary.Update()

SharePoint document libraries may have folders to better organize the contents of the library. These folders can be created using the same AddItem() method just as we did when adding new list items. The difference is that we use another overload definition of this method, which also accepts a value of type Microsoft.SharePoint.SPFileSystemObjectType that instructs it whether the new item is a file or a folder:

PS > $spFolder = $spDocumentLibrary.AddItem(
>> “”,[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,“My New Folder”
>>)
PS > $spFolder.Update()
Uploading Files to a Document Library

To upload files to a SharePoint document library, we use the Add method provided by the Microsoft.SharePoint.SPFileCollection class, which represents a collection of SPFile objects in SharePoint 2010.

Before we can access a file collection in SharePoint 2010, we have to create an instance of the Microsoft.SharePoint.SPFolder class using the GetFolder() method provided by the Microsoft.SharePoint.SPWeb class:

PS > $spFolder = $spWeb.GetFolder(“My Documents”)

After we have bound to the document library, we can store the file collection in a new variable, which we will use to add files:

PS > $spFileCollection = $spFolder.Files

The Add method provided by the Microsoft.SharePoint.SPFileCollection class is used to create a file in a file collection. This is a very versatile method that has 21 overload definitions. For the one that we will be using in our example, we need to specify the file’s relative URL, a byte array containing the file, and a Boolean value that determines whether an existing file with the same name that might already exist should be overwritten. Let’s have a look at the byte array first.

It is possible to expose a sequence of bytes using the System.IO.FileStream class, which we can pass on to the Add method. A simple way of retrieving an object of the type System.IO.FileStream is by using the OpenRead() method provided by the System.IO.FileInfo class. When using the Get-ChildItem cmdlet on a file, we get an object of the type System.IO.FileInfo:

PS > $file = Get-ChildItem C:DocumentsMyDoc.docx

Now we can use the OpenRead() method when adding a new file to a SharePoint library:

PS > $spFileCollection.Add(“My Documents/MyDoc.docx”,$file.OpenRead(),$false)

The example demonstrates how to upload a single file to a document library in SharePoint 2010. But what if we want to upload multiple files? Simply use the ForEach-Object cmdlet to loop through a collection of files and add them to a SharePoint 2010 document library using the Add() method:

PS > Get-ChildItem C:Documents -filter “*.docx” | ForEach {
>> $spFileCollection.Add(“My Documents/$($_.Name)”,$_.OpenRead(),$true)
>>}
Summary

In this post we’ve covered document libraries and uploading files using Windows PowerShell. We’ve seen examples of creating and managing document libraries and how to upload single and multiple files to a document library. The post only touches the surface of what you can do to manage your SharePoint 2010 environment. Be sure to check out PowerShell for Microsoft SharePoint 2010 Administrators for detailed examples about how to create document libraries, manage files, copy files between document libraries, check in and out files, and manage content types.

MK, that is all there is to using Windows PowerShell cmdlets to manage document libraries in SharePoint 2010. And this Guest Blogger Week is done. Our thanks to Niklas Goude for sharing all these exciting posts about SharePoint and Windows PowerShell. Join us tomorrow for Quick-Hits Friday.

We would love you to follow us on Twitter and Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys