{"id":17041,"date":"2010-09-22T00:01:00","date_gmt":"2010-09-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/22\/use-powershell-to-manage-lists-views-and-items-in-sharepoint\/"},"modified":"2010-09-22T00:01:00","modified_gmt":"2010-09-22T00:01:00","slug":"use-powershell-to-manage-lists-views-and-items-in-sharepoint","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-manage-lists-views-and-items-in-sharepoint\/","title":{"rendered":"Use PowerShell to Manage Lists, Views, and Items in SharePoint"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong>: Microsoft Scripting Guys guest blogger Niklas Goude discusses using Windows PowerShell cmdlets to manage lists, views, and items in SharePoint 2010.<\/p>\n<p>&nbsp;<\/p>\n<p><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! How do I manage lists, views, and items using Windows PowerShell and SharePoint?<\/p>\n<p>&#8212; NG<\/p>\n<p>&nbsp;<\/p>\n<p><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 NG, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. Same answer different day from me: Let&rsquo;s ask Niklas Goude, our guest blogger this week.<\/p>\n<p>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, <a href=\"http:\/\/powershell.se\">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 Chapters 14 and 15 of that book.<\/p>\n<p>Niklas also runs the blog, <a href=\"http:\/\/powershell.nu\">powershell.nu<\/a>, where he shares scripts, examples, and solutions for administrative tasks in Windows environments through Windows PowerShell.<\/p>\n<p>&nbsp;<\/p>\n<h5>Creating Lists in SharePoint 2010<\/h5>\n<p>One of the most powerful features of SharePoint from an end user&rsquo;s perspective is the ease of creating and customizing lists, views, and items. Lists store data. SharePoint 2010 includes a large number of list templates that can be used as they are or as a starting point to tailor them to fulfill your specific business requirements. Lists have a set of configurable settings that apply to all lists, as well as custom settings that apply only to the specific type of list used. <\/p>\n<p>Let&rsquo;s take a look at the templates available. We can do this using the <strong>ListTemplates<\/strong> property on an object of the type <strong>SPWeb<\/strong>. First, we store an object of the type <strong>SPWeb<\/strong> in a variable:<\/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\">$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> <\/div>\n<\/blockquote>\n<p>Next, we use the <strong>ListTemplates<\/strong> property and select the name and description, as shown in the following image. <\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/\"><img decoding=\"async\" height=\"277\" width=\"554\" src=\"https:\/\/docs.microsoft.com\/\" alt=\"Image of selecting name and description with ListTemplates property\" border=\"0\" title=\"Image of selecting name and description with ListTemplates property\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>We can use a template when creating a new list (of course we&rsquo;ll do this by using Windows PowerShell). When creating a list in SharePoint 2010, you use the <strong>Add<\/strong> method of the <strong>SPListCollection<\/strong> class. This method has seven overloads (method signatures, or ways in which the method can be called). The one we will be using for our example accepts three parameters: the list <strong>title<\/strong>, <strong>description<\/strong>, and <strong>template <\/strong>type to be used. The <strong>title<\/strong> and <strong>description<\/strong> parameters are both of the type <strong>System.String<\/strong>, and the <strong>template<\/strong> parameter is of the type <strong>SPListTemplateType<\/strong>, so we need to provide an instance of this type as a value for this parameter.<\/p>\n<p>Here, we create a variable containing the <strong>Contacts<\/strong> list template type, which we obtain from the <strong>Microsoft.SharePoint.SPListTemplateType<\/strong> enumeration:<\/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\">$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]::Contacts<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/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\">$spListCollection<\/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\">.Lists<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/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\">$spListCollection<\/span><span style=\"color: #000000\">.Add(<\/span><span style=\"color: #808080\">&#8220;My Contacts&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&#8220;Description&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$listTemplate<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>Why do we need this intermediary <strong>spListCollection<\/strong> variable? Why not call the <strong>Add<\/strong> method directly with <strong>$spWeb.Lists.Add<\/strong>? If we used the <strong>Add<\/strong> method directly and repeated the command ten times, the metadata for all available lists in the site would be loaded ten times. Storing the <strong>lists<\/strong> collection in a variable and working with that variable minimizes the amount of memory consumed, because the lists are loaded only once.<\/p>\n<h5>Modifying Lists in SharePoint 2010<\/h5>\n<p>Let&rsquo;s see how we can modify our new list. First, we get the list using the <strong>GetLists()<\/strong> method and store it in a variable:<\/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\">$spList<\/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;\/Lists\/My Contacts&#8221;<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>With the list stored in a variable, we can go ahead and set all kinds of cool stuff. Here&rsquo;s how to add a list to the Quick Launch:<\/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\">$spList<\/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; <\/p>\n<p><\/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\">$spList<\/span><span style=\"color: #000000\">.Update()<\/span> <\/div>\n<\/blockquote>\n<p>When you run the command above, the list appears on the Quick Launch. Setting the value to <strong>False<\/strong> hides the list from the Quick Launch. If we want to change the list&rsquo;s description, we can simply use the <strong>Description<\/strong> property:<\/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\">$spList<\/span><span style=\"color: #000000\">.Description<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;My Contact List&#8221; <\/p>\n<p><\/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\">$spList<\/span><span style=\"color: #000000\">.Update()<\/span> <\/div>\n<\/blockquote>\n<h5>Adding List Fields<\/h5>\n<p>It&rsquo;s also possible to add additional fields to a list. To create a field in a SharePoint 2010 list, use the <strong>Add<\/strong> method provided by the <strong>SPFieldCollection<\/strong> class. Here, we create a simple <strong>Text<\/strong> type field in a SharePoint list:<\/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\">$spFieldType<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[Microsoft.SharePoint.SPFieldType]::Text<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/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\">$spList<\/span><span style=\"color: #000000\">.Fields.Add(<\/span><span style=\"color: #808080\">&#8220;TextField&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$spFieldType<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>In this example, we create an <strong>SPFieldType<\/strong> object with the value <strong>Text<\/strong> and store it in the variable <strong>spFieldType<\/strong>. We then use the <strong>Add<\/strong> method and pass in the field&rsquo;s display name, followed by the variable <strong>spFieldType<\/strong>, followed by Boolean <strong>False<\/strong>. The last parameter in this overload of the <strong>Add<\/strong> method specifies whether the new field is required to always contain a value. Our example creates a new text field in the list with the display name of <strong>TextField<\/strong> that will not require any input. An additional Boolean parameter you can use with the <strong>Add<\/strong> method compacts the field name to eight characters, if set to <strong>True<\/strong>.<\/p>\n<p>SharePoint 2010 supports other types of fields as well. Adding a Choice field is a little different since it requires additional information regarding the possible choices. You can store the choices in an instance of the System.Collections.Specialized.StringCollection class, as shown in this example:<\/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\">$choices<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">System.Collections.Specialized.StringCollection<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/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\">$choices<\/span><span style=\"color: #000000\">.Add(<\/span><span style=\"color: #808080\">&#8220;First Choice&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/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\">$choices<\/span><span style=\"color: #000000\">.Add(<\/span><span style=\"color: #808080\">&#8220;Second Choice&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/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\">$choices<\/span><span style=\"color: #000000\">.Add(<\/span><span style=\"color: #808080\">&#8220;Third Choice&#8221;<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>Now that we have our choices stored in a variable, we can use the variable when creating a <strong>Choice<\/strong> type field:<\/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\">$spFieldType<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[Microsoft.SharePoint.SPFieldType]::Choice<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/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\">$spList<\/span><span style=\"color: #000000\">.Fields.Add(<\/span><span style=\"color: #808080\">&#8220;ChoiceField&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$spFieldType<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$choices<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>We use the <strong>choices<\/strong> variable to associate a list of options with the field.<\/p>\n<h5>Managing List Views<\/h5>\n<p>Let&rsquo;s move on and look at how to manage list views in SharePoint 2010. Views enable you to create customized representations of list data for specific purposes, such as displaying specific fields. When a new list in SharePoint is created, a default view is added. Document Library lists get the All Documents view, Picture Library lists get the All Pictures view, and most of the other list types get the All Items view. <\/p>\n<p>Before we can edit an existing view in SharePoint 2010, we need to retrieve it. We can use the <strong>GetViewFromUrl<\/strong> method of the <strong>SPWeb<\/strong> class. Let&rsquo;s get the <strong>Contacts<\/strong> list we created earlier:<\/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\">$spView<\/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\">.GetViewFromUrl(<\/span><span style=\"color: #808080\">&#8220;\/Lists\/My Contacts\/AllItems.aspx&#8221;<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>When new fields are created, they are not added to a view by default. We can, of course, add a field to a view by using Windows PowerShell. First, we need to create a reference to the field that we want to add to the view:<\/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\">$spField<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spList<\/span><span style=\"color: #000000\">.Fields[<\/span><span style=\"color: #808080\">&#8220;TextField&#8221;<\/span><span style=\"color: #000000\">]<\/span> <\/div>\n<\/blockquote>\n<p>We then can use the <strong>Add<\/strong> method provided by the <strong>SPViewFieldCollection<\/strong> class to add the field to a view and finally use the <strong>Update()<\/strong> method to set 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\">$spView<\/span><span style=\"color: #000000\">.ViewFields.Add(<\/span><span style=\"color: #2b91af\">$spField<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <\/p>\n<p><\/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\">$spView<\/span><span style=\"color: #000000\">.Update()<\/span> <\/div>\n<\/blockquote>\n<p>You can do a lot more cool stuff with views such as set custom queries, create new views, and remove views.<\/p>\n<h5>Managing List Items<\/h5>\n<p>Let&rsquo;s move ahead and take a look at how to add list items to a list. The <strong>SPList<\/strong> class provides the <strong>AddItem<\/strong> method, which is used to create new list items. When you call the <strong>AddItem<\/strong> method, an object of the type <strong>Microsoft.SharePoint.SPListItem<\/strong> is returned. Because we want to populate the properties of the new list item, we need to store the object in a variable in order to continue to work with it:<\/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\">$spListItem<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spList<\/span><span style=\"color: #000000\">.AddItem()<\/span> <\/div>\n<\/blockquote>\n<p>Now we can start assigning values to the different fields the corresponding list item inherits from the parent list. The <strong>SPListItem<\/strong> class provides a parameterized <strong>Item<\/strong> property for accessing the value contained in a particular field. To specify the value of the <strong>Title<\/strong> field, we can use the following:<\/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\">$spListItem<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #808080\">&#8220;Title&#8221;<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;New Item&#8221;<\/span> <\/div>\n<\/blockquote>\n<p>We can assign additional values to fields as well. In the next example, we add values to the <strong>TextField<\/strong> and <strong>ChoiceField<\/strong>, which we created earlier in this example:<\/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\">$spListItem<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #808080\">&#8220;TextField&#8221;<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;Hey hey&#8221; <\/p>\n<p><\/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\">$spListItem<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #808080\">&#8220;ChoiceField&#8221;<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;First Choice&#8221;<\/span> <\/div>\n<\/blockquote>\n<p>Finally, we use the <strong>Update()<\/strong> method to create the new list item:<\/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\">$spListItem<\/span><span style=\"color: #000000\">.Update()<\/span> <\/div>\n<\/blockquote>\n<p>What if we want to update an existing list item? Well, the <strong>SPList<\/strong> class provides a few methods we can use to retrieve a specific from a list. The most common methods are <strong>GetItemById()<\/strong> and <strong>GetItems()<\/strong>. <\/p>\n<p>Wait a minute! Isn&rsquo;t it simpler to loop through the <strong>Items<\/strong> collection and create a filter using the <strong>Where-Object<\/strong> cmdlet? It is simpler and does feel like the Windows PowerShell way of doing things, but consider this: When you use the <strong>Items<\/strong> property on an <strong>SPList<\/strong> object, all the list items in the list are read into memory, meaning that large lists may consume a lot of memory. A better approach is to use either the <strong>GetItemsById()<\/strong> method or the <strong>GetItems()<\/strong> method to minimize memory consumption.<\/p>\n<p>The <strong>GetItemById()<\/strong> method requires that we know the ID of the particular item. In many cases, we have no idea of an item&rsquo;s ID, but we might know the item&rsquo;s title or some other value. This is where the <strong>GetItems()<\/strong> method becomes useful. The method returns all list items or a subset of list items as defined by search criteria in a CAML query. <\/p>\n<p>To use a CAML query with the <strong>GetItems<\/strong> method, we first need to create an object of the type <strong>Microsoft.SharePoint.SPQuery<\/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\">$spQuery<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Microsoft.SharePoint.SPQuery<\/span> <\/div>\n<\/blockquote>\n<p>The <strong>SPQuery<\/strong> object supports the <strong>Query<\/strong> property, which we use to place a CAML query. Here&rsquo;s the CAML query we&rsquo;ll use in our example:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">$camlQuery<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&lsquo;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Name<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&#8220;Title&#8221; <\/span><span style=\"color: #000000\">\/&gt;&lt;Value<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">Type<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&#8220;Text&#8221;<\/span><span style=\"color: #000000\">&gt;True&lt;\/Value&gt;&lt;\/Eq&gt;&lt;\/Where&gt;<\/span><span style=\"color: #808080\">&#8216;<\/span> <\/div>\n<\/blockquote>\n<p>We create a new query containing a <strong>Where<\/strong> statement using the <strong>&lt;Where&gt;<\/strong> tag. Next, we specify an equals expression using the <strong>&lt;Eq&gt;<\/strong> tag. For other types of searches, you can replace this tag with the appropriate one, such as <strong>&lt;Lt&gt;<\/strong> or <strong>&lt;Gt&gt;<\/strong> to search for list items where the value of this field is less than or greater than a value, respectively.<\/p>\n<p>We then specify the field we want to query against using the <strong>&lt;FieldRef&gt;<\/strong> tag. In this example, we want to look at the <strong>Title<\/strong> field in the list. Finally, we use the <strong>&lt;Value&gt;<\/strong> tag to specify that the value type is <strong>Text<\/strong> and that the value should equal <strong>New Item<\/strong>.<\/p>\n<p>After we have created a CAML query and stored it in a variable, we assign it to the <strong>Query<\/strong> property of our <strong>SPQuery<\/strong> object:<\/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\">$spQuery<\/span><span style=\"color: #000000\">.Query<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$camlQuery<\/span> <\/div>\n<\/blockquote>\n<p>Before using the <strong>SPQuery<\/strong> with the <strong>GetItems<\/strong> method, we should specify the <strong>RowLimit<\/strong> property that is used to limit the amount of items returned per page. If we run the <strong>SPQuery<\/strong> without setting the row limit, the query will select all items matching the criteria and might fail on lists with a large number of items:<\/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\">$spQuery<\/span><span style=\"color: #000000\">.RowLimit<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">100<\/span> <\/div>\n<\/blockquote>\n<p>Here, we set the <strong>RowLimit<\/strong> property to 100 so that only 100 list items are returned per page. The <strong>RowLimit<\/strong> value should be between 1 and 2000. Finally, we can call the <strong>GetItems()<\/strong> method with the <strong>SPQuery<\/strong> object instance we created earlier for input:<\/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\">$spListItem<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$spList<\/span><span style=\"color: #000000\">.GetItems(<\/span><span style=\"color: #2b91af\">$spQuery<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>Now it&rsquo;s a simple task to update the list item. In the example below, we change the item&rsquo;s <strong>Title<\/strong>. Note that the <strong>GetItems()<\/strong> method returns a <strong>ListItemCollection<\/strong>, so even if only one value is returned, we still have to index the first element or use the <strong>ForEach-Object<\/strong> cmdlet to loop through each item:<\/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\">$spListItemCollection<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">ForEach-Object<\/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\">$_<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #808080\">&#8220;Title&#8221;<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;New Value&#8221;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.Update()<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">&gt;&gt;}<\/span> <\/div>\n<\/blockquote>\n<h5>Summary<\/h5>\n<p>In this post, we&rsquo;ve looked at how to manage lists, views, and items by using Windows PowerShell. We saw examples about how to create new lists, modify list properties, add fields, manage views, and how to work with items. Chapter 14 in <i>PowerShell for SharePoint 2010 Administrators <\/i>(available October 2010) takes list and view management one step further by showing how to manage lookup fields, how to retrieve lists using a simple function, how to automate list creation, and much more. In Chapter 15, you&rsquo;ll see detailed examples about how to create, manage, delete, and copy list Items.<\/p>\n<p>&nbsp;<\/p>\n<p>NG, that is all there is to managing lists, views, and items by using Windows PowerShell and SharePoint. Guest Blogger Week with Niklas Goude and SharePoint will continue tomorrow.<\/p>\n<p>We would love for 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=\"mailto: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.<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Microsoft Scripting Guys guest blogger Niklas Goude discusses using Windows PowerShell cmdlets to manage lists, views, and items in SharePoint 2010. &nbsp; Hey, Scripting Guy! How do I manage lists, views, and items using Windows PowerShell and SharePoint? &#8212; NG &nbsp; Hello NG, Microsoft Scripting Guy Ed Wilson here. Same answer different day [&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-17041","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: Microsoft Scripting Guys guest blogger Niklas Goude discusses using Windows PowerShell cmdlets to manage lists, views, and items in SharePoint 2010. &nbsp; Hey, Scripting Guy! How do I manage lists, views, and items using Windows PowerShell and SharePoint? &#8212; NG &nbsp; Hello NG, Microsoft Scripting Guy Ed Wilson here. Same answer different day [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17041","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=17041"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17041\/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=17041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}