{"id":17031,"date":"2010-09-23T00:01:00","date_gmt":"2010-09-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/23\/use-powershell-cmdlets-to-manage-sharepoint-document-libraries\/"},"modified":"2010-09-23T00:01:00","modified_gmt":"2010-09-23T00:01:00","slug":"use-powershell-cmdlets-to-manage-sharepoint-document-libraries","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-cmdlets-to-manage-sharepoint-document-libraries\/","title":{"rendered":"Use PowerShell Cmdlets to Manage SharePoint Document Libraries"},"content":{"rendered":"<p>&nbsp;\n<strong>Summary<\/strong>: Management of SharePoint 2010 document libraries by using Windows PowerShell cmdlets is explored in this Hey, Scripting Guy! Blog post.\n&nbsp;\n<img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\"> Hey, Scripting Guy! Please tell me about document management with SharePoint and Windows PowerShell.\n&#8212; MK\n&nbsp;\n<img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\"> Hello MK,\nMicrosoft 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.\nNiklas 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, <a href=\"http:\/\/powershell.se\">http:\/\/powershell.se<\/a>, and is currently co-authoring a book with Mattias Karlsson titled,<a href=\"http:\/\/www.amazon.com\/PowerShell-Microsoft-SharePoint-2010-Administrators\/dp\/0071747974\"> <i>PowerShell for Microsoft SharePoint 2010 Administrators<\/i><\/a>, which will be published in English by McGraw-Hill in October 2010.&nbsp;Parts of this post are taken from Chapter 16 of that book.\nNiklas also runs the blog, <a href=\"http:\/\/powershell.nu\">http:\/\/powershell.nu<\/a>, where he shares scripts, examples, and solutions for administrative tasks in Windows environments through Windows PowerShell.\n&nbsp;<\/p>\n<h5>Creating Document Libraries<\/h5>\n<p>Working with document libraries is similar to working with SharePoint lists, as described in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/22\/use-powershell-to-manage-lists-views-and-items-in-sharepoint.aspx\">yesterday&rsquo;s post<\/a>. In this post we will create a new document library and see examples of how to upload documents to a document library.\nCreating a new document library using Windows PowerShell is very similar to the creation of any other type of list. We can use the same <strong>Add() <\/strong>method provided by the <strong>SPListCollection<\/strong> class. In the example below, we use the <strong>Get-SPWeb<\/strong> cmdlet to retrieve a specific site, store a <strong>TemplateType<\/strong> in a variable, and then use the <strong>Add()<\/strong> method to create a new document library:<\/p>\n<blockquote><p>  <span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spWeb<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-SPWeb<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Identity<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">http:\/\/SPServer<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$listTemplate<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spWeb<\/span><span style=\"color: #000000\">.Lists.Add(<\/span><span style=\"color: #808080\">&#8220;My Documents&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&#8220;My Doc Library&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$listTemplate<\/span><span style=\"color: #000000\">)<\/span> <\/p>\n<\/blockquote>\n<h5>Modifying a Document Library<\/h5>\n<p>We can retrieve an existing document library using the <strong>GetList()<\/strong> method, just as we did with lists. The only difference is the relative URL used with the method:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spDocumentLibrary<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spWeb<\/span><span style=\"color: #000000\">.GetList(<\/span><span style=\"color: #808080\">&#8220;My Documents&#8221;<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>Next, we can modify the properties of a document library. If we want to change the <strong>Description<\/strong>, we can simply type:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spDocumentLibrary<\/span><span style=\"color: #000000\">.Description<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&ldquo;Lots<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">of<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Documents&rdquo;<\/span> <\/div>\n<\/blockquote>\n<p>Adding <strong>Document Library<\/strong> to Quick Launch is also a simple task when using Windows PowerShell:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spDocumentLibrary<\/span><span style=\"color: #000000\">.OnQuickLaunch<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;True&#8221;<\/span> <\/div>\n<\/blockquote>\n<p>When we&rsquo;re are done with the updates we use the <strong>Update() <\/strong>method to commit the changes.<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spDocumentLibrary<\/span><span style=\"color: #000000\">.Update()<\/span> <\/div>\n<\/blockquote>\n<p>SharePoint document libraries may have folders to better organize the contents of the library. These folders can be created using the same <strong>AddItem()<\/strong> 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 <strong>Microsoft.SharePoint.SPFileSystemObjectType<\/strong> that instructs it whether the new item is a file or a folder:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spFolder<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spDocumentLibrary<\/span><span style=\"color: #000000\">.AddItem(<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\"> &#8220;&#8221;<\/span><span style=\"color: #000000\">,[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,<\/span><span style=\"color: #808080\">&#8220;My New Folder&#8221; <br \/><\/span><span style=\"color: #000000\">&gt;&gt;)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spFolder<\/span><span style=\"color: #000000\">.Update()<\/span> <\/div>\n<\/blockquote>\n<h5>Uploading Files to a Document Library<\/h5>\n<p>To upload files to a SharePoint document library, we use the <strong>Add<\/strong> method provided by the <strong>Microsoft.SharePoint.SPFileCollection<\/strong> class, which represents a collection of <strong>SPFile<\/strong> objects in SharePoint 2010.\nBefore we can access a file collection in SharePoint 2010, we have to create an instance of the <strong>Microsoft.SharePoint.SPFolder<\/strong> class using the <strong>GetFolder()<\/strong> method provided by the <strong>Microsoft.SharePoint.SPWeb<\/strong> class:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spFolder<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spWeb<\/span><span style=\"color: #000000\">.GetFolder(<\/span><span style=\"color: #808080\">&#8220;My Documents&#8221;<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>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:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spFileCollection<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spFolder<\/span><span style=\"color: #000000\">.Files<\/span> <\/div>\n<\/blockquote>\n<p>The <strong>Add<\/strong> method provided by the <strong>Microsoft.SharePoint.SPFileCollection<\/strong> 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&rsquo;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&rsquo;s have a look at the byte array first.\nIt is possible to expose a sequence of bytes using the <strong>System.IO.FileStream<\/strong> class, which we can pass on to the <strong>Add<\/strong> method. A simple way of retrieving an object of the type <strong>System.IO.FileStream<\/strong> is by using the <strong>OpenRead()<\/strong> method provided by the <strong>System.IO.FileInfo<\/strong> class. When using the <strong>Get-ChildItem<\/strong> cmdlet on a file, we get an object of the type <strong>System.IO.FileInfo<\/strong>:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$file<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-ChildItem<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:DocumentsMyDoc.docx<\/span> <\/div>\n<\/blockquote>\n<p>Now we can use the <strong>OpenRead()<\/strong> method when adding a new file to a SharePoint library:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spFileCollection<\/span><span style=\"color: #000000\">.Add(<\/span><span style=\"color: #808080\">&#8220;My Documents\/MyDoc.docx&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$file<\/span><span style=\"color: #000000\">.OpenRead(),<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>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 <strong>ForEach-Object<\/strong> cmdlet to loop through a collection of files and add them to a SharePoint 2010 document library using the <strong>Add()<\/strong> method:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-ChildItem<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:Documents<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-filter<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&ldquo;*.docx&rdquo;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">ForEach<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spFileCollection<\/span><span style=\"color: #000000\">.Add(&ldquo;My<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Documents\/<\/span><span style=\"color: #2b91af\">$<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.Name)&rdquo;,<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.OpenRead(),<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">&gt;&gt;}<\/span> <\/div>\n<\/blockquote>\n<h5>Summary<\/h5>\n<p>In this post we&rsquo;ve covered document libraries and uploading files using Windows PowerShell. We&rsquo;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 <i>PowerShell for Microsoft SharePoint 2010 Administrators<\/i> 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.\nMK, 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.\nWe would love you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Management of SharePoint 2010 document libraries by using Windows PowerShell cmdlets is explored in this Hey, Scripting Guy! Blog post. &nbsp; Hey, Scripting Guy! Please tell me about document management with SharePoint and Windows PowerShell. &#8212; MK &nbsp; Hello MK, Microsoft Scripting Guy Ed Wilson here. It is Thursday in Charlotte, North Carolina, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,183,3,59,45],"class_list":["post-17031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-niklas-goude","tag-scripting-guy","tag-sharepoint","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Management of SharePoint 2010 document libraries by using Windows PowerShell cmdlets is explored in this Hey, Scripting Guy! Blog post. &nbsp; Hey, Scripting Guy! Please tell me about document management with SharePoint and Windows PowerShell. &#8212; MK &nbsp; Hello MK, Microsoft Scripting Guy Ed Wilson here. It is Thursday in Charlotte, North Carolina, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17031"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17031\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}