{"id":17051,"date":"2010-09-21T00:01:00","date_gmt":"2010-09-21T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/21\/use-sharepoint-2010-powershell-cmdlets-to-get-and-manage-sites\/"},"modified":"2010-09-21T00:01:00","modified_gmt":"2010-09-21T00:01:00","slug":"use-sharepoint-2010-powershell-cmdlets-to-get-and-manage-sites","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-sharepoint-2010-powershell-cmdlets-to-get-and-manage-sites\/","title":{"rendered":"Use SharePoint 2010 PowerShell Cmdlets to Get and Manage Sites"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong>: The Microsoft Scripting Guys host guest blogger Niklas Goude as he discusses site management using Windows PowerShell cmdlets.<\/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 sites in SharePoint 2010 by using Windows PowerShell cmdlets?<\/p>\n<p>&#8212; KM<\/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 KM, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here, once again I will defer the question to our guest blogger for the week, Niklas Goude. <\/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, <i><a href=\"http:\/\/www.amazon.com\/PowerShell-Microsoft-SharePoint-2010-Administrators\/dp\/0071747974\">PowerShell for Microsoft SharePoint 2010 Administrators<\/a><\/i>, which will be published in English by McGraw-Hill in October 2010. Parts of this post are taken from Chapter 4 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>Getting Sites in SharePoint 2010<\/h5>\n<p>Let&rsquo;s see how we can manage Sites in a SharePoint 2010 environment. As discussed in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/20\/get-started-managing-sharepoint-2010-with-powershell-cmdlets.aspx\">yesterday&rsquo;s post<\/a>, SharePoint 2010 includes more than 500 cmdlets. Some of these are designed to manage site collections and sites. A site in SharePoint 2010 is hosted within a site collection and contains one or more web pages, lists, or libraries to store and present information. The first cmdlet we&rsquo;ll look at is <strong>Get-SPWeb<\/strong>, which is used to retrieve one or more sites. Here&rsquo;s an example of using the cmdlet:<\/p>\n<blockquote>\n<div class=\"code\"><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>Get-SPWeb<\/span><span>&nbsp;<\/span><span>-Identity<\/span><span>&nbsp;<\/span><span>http:\/\/SPServer<\/span><span> <\/p>\n<p><\/span><span>Url<\/span><span> <\/p>\n<p><\/span><span>&#8212;<\/span><span> <\/p>\n<p><\/span><span>http:\/\/spserver<\/span> <\/div>\n<\/blockquote>\n<p>The <strong>Get-SPWeb<\/strong> cmdlet displays the <strong>Url<\/strong> property by default. We can display all properties using the <strong>Format-List<\/strong> cmdlet as demonstrated in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1512.HSG09211001_12CB08CE.jpg\"><img decoding=\"async\" height=\"277\" width=\"554\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7776.HSG09211001_thumb_4239779A.jpg\" alt=\"Image of Format-List cmdlet displaying all properties\" border=\"0\" title=\"Image of Format-List cmdlet displaying all properties\" 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>The example above displays a lot of cool properties such as <strong>Title<\/strong>, <strong>Description<\/strong>, <strong>Language<\/strong>, when the site was created, and when the last item was modified. <\/p>\n<p><strong>Modifying Sites in SharePoint 2010<\/strong><\/p>\n<p>If we want to modify specific properties, we can use the <strong>Set-SPWeb<\/strong> cmdlet. Here&rsquo;s how to change the description of a site:<\/p>\n<blockquote>\n<div class=\"code\"><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>Get-SPWeb<\/span><span>&nbsp;<\/span><span>-Identity<\/span><span>&nbsp;<\/span><span>http:\/\/SPServer<\/span><span>&nbsp;<\/span><span>|<\/span><span>&nbsp;<\/span><span>Set-SPWeb<\/span><span>&nbsp;<\/span><span>-Description<\/span><span> &#8220;PowerShell is Cool&#8221;<\/span> <\/div>\n<\/blockquote>\n<p>You can use the <strong>Set-SPWeb<\/strong> cmdlet to set some of the properties. At some point you&rsquo;ll want to change additional properties that are not supported by the <strong>Set-SPWeb<\/strong> cmdlet. You can do this by storing a site in a variable and then set the properties:<\/p>\n<blockquote>\n<div class=\"code\"><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>$spWeb<\/span><span>&nbsp;<\/span><span>=<\/span><span>&nbsp;<\/span><span>Get-SPWeb<\/span><span>&nbsp;<\/span><span>-Identity<\/span><span>&nbsp;<\/span><span>http:\/\/SPServer<\/span><span> <\/p>\n<p><\/span><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>$spWeb<\/span><span>.Title<\/span><span>&nbsp;<\/span><span>=<\/span><span> &#8220;PowerShell&#8221; <\/p>\n<p><\/span><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>$spWeb<\/span><span>.TreeViewEnabled<\/span><span>&nbsp;<\/span><span>=<\/span><span> &#8220;True&#8221; <\/p>\n<p><\/span><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>$spWeb<\/span><span>.Update()<\/span> <\/div>\n<\/blockquote>\n<p>In the example above, we change the sites title and enable the <strong>TreeView<\/strong>. Finally, we use the <strong>Update()<\/strong> method to commit the changes. Just as with objects of the type <strong>SPSite<\/strong>, <strong>SPWeb<\/strong> objects need to be disposed of correctly when stored in variables using either the <strong>Dispose()<\/strong> method or the <strong>Start-SPAssignment<\/strong> and <strong>Stop-SPAssignment<\/strong> cmdlets. The example below demonstrates how to use the <strong>Dispose()<\/strong> method:<\/p>\n<blockquote>\n<div class=\"code\"><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>$spWeb<\/span><span>.Dispose()<\/span> <\/div>\n<\/blockquote>\n<p>Windows PowerShell makes it real simple to update multiple sites in a site collection through a couple of lines of code. Here&rsquo;s how to enable the <strong>TreeView<\/strong> on all sites in a site collection:<\/p>\n<blockquote>\n<div class=\"code\"><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>Get-SPSite<\/span><span>&nbsp;<\/span><span>-Identity<\/span><span>&nbsp;<\/span><span>http:\/\/SPServer<\/span><span>&nbsp;<\/span><span>|<\/span><span>&nbsp;<\/span><span>Get-SPWeb<\/span><span>&nbsp;<\/span><span>|<\/span><span>&nbsp;<\/span><span>ForEach-Object<\/span><span>&nbsp;<\/span><span>{<\/span><span> <\/p>\n<p><\/span><span>&gt;&gt;<\/span><span>&nbsp;<\/span><span>$_<\/span><span>.TreeViewEnabled<\/span><span>&nbsp;<\/span><span>=<\/span><span>&nbsp;<\/span><span>&ldquo;True&rdquo;<\/span><span> <\/p>\n<p><\/span><span>&gt;&gt;<\/span><span>&nbsp;<\/span><span>$_<\/span><span>.Update()<\/span><span> <\/p>\n<p><\/span><span>&gt;&gt;<\/span><span>&nbsp;<\/span><span>}<\/span> <\/div>\n<\/blockquote>\n<p>In the example above we use the <strong>ForEach-Object<\/strong> cmdlet to iterate through each site in the site collection and enable the <strong>TreeView<\/strong> and finally call the <strong>Update()<\/strong> method to commit the changes. <\/p>\n<h5>Creating Sites in SharePoint 2010<\/h5>\n<p>Creating a new site is a simple procedure when using Windows PowerShell. Simply use the <strong>New-SPWeb<\/strong> cmdlet as demonstrated below.<\/p>\n<blockquote>\n<div class=\"code\"><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>New-SPWeb<\/span><span>&nbsp;<\/span><span>-Url<\/span><span>&nbsp;<\/span><span>http:\/\/SPServer\/NewWeb<\/span><span>&nbsp;<\/span><span>-Template<\/span><span> &#8220;STS#0&#8221; <\/span><span>-Name<\/span><span> &#8220;New Site&#8221; <\/span><span>`<\/span><span> <br \/><\/span><span>&gt;&gt;<\/span><span>&nbsp;<\/span><span>-Description<\/span><span> &#8220;My New Site&#8221; <\/p>\n<p><\/span><span>Url<\/span><span> <\/p>\n<p><\/span><span>&#8212;<\/span><span> <\/p>\n<p><\/span><span>http:\/\/spserver\/NewWeb<\/span> <\/div>\n<\/blockquote>\n<p>In the example above we set the site&rsquo;s new URL and set the Template to <strong>&ldquo;STS#0&rdquo;<\/strong>, which corresponds to a team site. We also set the <strong>Sites Name<\/strong> and <strong>Description<\/strong>. <\/p>\n<h5><\/h5>\n<h5>Backup &amp; Restore Sites in SharePoint 2010<\/h5>\n<p>Let&rsquo;s take a closer look at backup and sestore. When working with sites, we can use the <strong>Export-SPWeb<\/strong> cmdlet to export a site:<\/p>\n<blockquote>\n<div class=\"code\"><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>Export-SPWeb<\/span><span>&nbsp;<\/span><span>-Identity<\/span><span>&nbsp;<\/span><span>http:\/\/SPServer\/NewSite<\/span><span>&nbsp;<\/span><span>`<\/span><span> <\/p>\n<p><\/span><span>&gt;&gt;<\/span><span>&nbsp;<\/span><span>-Path<\/span><span>&nbsp;<\/span><span>C:\\Backup\\spWebBackup.bak<\/span> <\/div>\n<\/blockquote>\n<p>This example exports an entire site to a backup file. It is also possible to export specific content from a subsite, such as lists, document libraries, and list items. You use the <strong>ItemUrl<\/strong> parameter to export lists or list items from a subsite. Here is an example of exporting a list called <strong>Calendar<\/strong> from a site:<\/p>\n<blockquote>\n<div class=\"code\"><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>Export-SPWeb<\/span><span>&nbsp;<\/span><span>-Identity<\/span><span>&nbsp;<\/span><span>http:\/\/SPServer\/NewSite<\/span><span>&nbsp;<\/span><span>`<\/span><span> <\/p>\n<p><\/span><span>&gt;&gt;<\/span><span>&nbsp;<\/span><span>-ItemUrl<\/span><span> &#8220;Lists\/Calendar&#8221; <\/span><span>-Path<\/span><span>&nbsp;<\/span><span>C:\\Backup\\spWebCalendar.bak<\/span> <\/div>\n<\/blockquote>\n<p>The <strong>Export-SPWeb<\/strong> cmdlet also supports the <strong>IncludeUserSecurity<\/strong> switch parameter, which allows you to include access control lists for all items. By default, <strong>Export-SPWeb<\/strong> exports the last major version of a list item, but you can change this by setting the <strong>IncludeVersions<\/strong> parameter to include the current version, last major and minor version, or all versions of each item.<\/p>\n<p>After you have an export file, you can use the <strong>Import-SPWeb<\/strong> cmdlet to import it into a subsite. Importing a subsite works as long as you specify a site collection that contains a matching template; otherwise, an error occurs:<\/p>\n<blockquote>\n<div class=\"code\"><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>Import-SPWeb<\/span><span>&nbsp;<\/span><span>-Identity<\/span><span>&nbsp;<\/span><span>http:\/\/SPServer\/NewSite<\/span><span>&nbsp;<\/span><span>-Path<\/span><span>&nbsp;<\/span><span>C:\\Backup\\spWebCalendar.bak<\/span> <\/div>\n<\/blockquote>\n<p>The <strong>Import-SPWeb<\/strong> cmdlet also supports the <strong>UpdateVersions<\/strong> parameter, which allows you to specify how to handle items that already exist in a list. The possible values are <strong>Append<\/strong>, <strong>Overwrite<\/strong>, and <strong>Ignore<\/strong>.<\/p>\n<p><strong>Remove Sites in SharePoint 2010<\/strong><\/p>\n<p>Finally, let&rsquo;s take a look at how to remove an existing site. The <strong>Remove-SPWeb<\/strong> cmdlet removes a specific site from SharePoint 2010. If the top-level site is deleted, the site collection is also removed. Here is an example of running this cmdlet:<\/p>\n<blockquote>\n<div class=\"code\"><span>PS<\/span><span>&nbsp;<\/span><span>&gt;<\/span><span>&nbsp;<\/span><span>Remove-SPWeb<\/span><span>&nbsp;<\/span><span>-Identity<\/span><span>&nbsp;<\/span><span>http:\/\/SPServer\/NewSite<\/span><span>&nbsp;<\/span><span>-Confirm:<\/span><span>$false<\/span> <\/div>\n<\/blockquote>\n<h5>Summary<\/h5>\n<p>In this post we&rsquo;ve covered sites in SharePoint 2010, with examples showing us how to create, modify, backup and restore, and remove sites in SharePoint 2010. It&rsquo;s also possible to go one step further and automate the creation of sites using a user-friendly SharePoint list, manage lists and libraries, and even items. We&rsquo;ll see examples of this in the upcoming post. You should also check out <i>PowerShell for Microsoft SharePoint 2010 Administrators<\/i> for a detailed description of how you can manage your SharePoint 2010 environment using Windows PowerShell. (This book will be available in October 2010.)<\/p>\n<p>KM, that is all there is to using Windows PowerShell cmdlets to manage sites in SharePoint 2010. 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 e-mail 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>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: The Microsoft Scripting Guys host guest blogger Niklas Goude as he discusses site management using Windows PowerShell cmdlets. &nbsp; Hey, Scripting Guy! How do I manage sites in SharePoint 2010 by using Windows PowerShell cmdlets? &#8212; KM &nbsp; Hello KM, Microsoft Scripting Guy Ed Wilson here, once again I will defer the question [&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-17051","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: The Microsoft Scripting Guys host guest blogger Niklas Goude as he discusses site management using Windows PowerShell cmdlets. &nbsp; Hey, Scripting Guy! How do I manage sites in SharePoint 2010 by using Windows PowerShell cmdlets? &#8212; KM &nbsp; Hello KM, Microsoft Scripting Guy Ed Wilson here, once again I will defer the question [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17051","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=17051"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17051\/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=17051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}