{"id":1548,"date":"2012-01-24T06:43:13","date_gmt":"2012-01-24T06:43:13","guid":{"rendered":"http:\/\/www.obieta.com\/?p=1548"},"modified":"2012-01-24T06:43:13","modified_gmt":"2012-01-24T06:43:13","slug":"sharepoint-2010-powershell-cheatsheet","status":"publish","type":"post","link":"http:\/\/obieta.com\/?p=1548","title":{"rendered":"Sharepoint 2010 Powershell Cheatsheet"},"content":{"rendered":"<p>*Get local farm information using PowerShell<br \/>Using\u00a0 the Get-SPFarm cmdlet, you can retrieve information, including\u00a0 the name of the Config database, version number, and servers in the farm\u00a0 that can be useful when creating scripts for your SharePoint farm.<\/p>\n<p>You can open up the SharePoint 2010 Management Shell on your SharePoint server to view the settings of your farm and type:<\/p>\n<p>PS &gt; Get-SPFarm<br \/>You will\u00a0 only see the name of the configuration database. You can get more detailed information if you pipe the cmdlet to the format list:<\/p>\n<p>PS &gt; Get-SPFarm | Format-List<\/p>\n<p>* Quickly Access the SharePoint Hive<\/p>\n<p>If you use SharePoint on a daily basis, you are probably familiar with the SharePoint hive. In SharePoint 2010, the hive is called the SharePoint root. The SharePoint root is used by SharePoint to store features, logs, binary files, templates and much more. It\u2019s commonly used when performing administrative tasks in SharePoint. Changing location to the path is fun, the first time, but after typing the path 100 times, it gets a bit tedious. In Windows PowerShell you can solve this by adding a new PSDrive that points to the SharePoint Root.<\/p>\n<p>New-PSDrive -Name SharePoint -Root &#8220;C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14&#8221; -PSProvider FileSystem<\/p>\n<p>If you place the command in your profile it will always be loaded when you start PowerShell.<\/p>\n<p>Now you can change location to the SharePoint Root using a simple command:<\/p>\n<p>cd SharePoint:<br \/>If you want to open the folder in explorer, simply type:<\/p>\n<p>explorer (Convert-Path SharePoint:)<\/p>\n<p>&nbsp;<\/p>\n<p>* Find information about your SharePoint databases<\/p>\n<p>You can use the Get-SPDatabase cmdlet to display information about the SharePoint databases.<\/p>\n<p>PS &gt; Get-SPDatabase<\/p>\n<p>The example above displays all SharePoint databases. If you want to display a specific Database, use the identity parameter followed by the database ID.<\/p>\n<p>PS &gt; Get-SPDatabase -Identity<\/p>\n<p>* Hide a list in SharePoint 2010<\/p>\n<p>You can use Windows PowerShell to hide a list in SharePoint 2010. Simply store an instance of an SPList Object, set the Hidden property to $true and update the SPList object.<\/p>\n<p>PS &gt; $spWeb = Get-SPWeb <a href=\"http:\/\/sp01\/site\">http:\/\/sp01\/site<\/a><br \/>PS &gt; $spList = $spWeb.GetList(&#8220;<a href=\"http:\/\/sp01\/site\/Lists\/MyList\">http:\/\/sp01\/site\/Lists\/MyList<\/a>&#8220;)<br \/>PS &gt; $spList.Hidden = $true<br \/>PS &gt; $spList.Update()<\/p>\n<p>* Remove a list in SharePoint 2010 from the Quick Launch toolbar<\/p>\n<p>You can use Windows PowerShell to remove a list in SharePoint 2010 from the Quick Launch toolbar. Simply store an instance of an SPList Object, set the OnQuickLaunch property to $false, and update the SPList object.<\/p>\n<p>PS &gt; $spWeb = Get-SPWeb <a href=\"http:\/\/sp01\/site\">http:\/\/sp01\/site<\/a><br \/>PS &gt; $spList = $spWeb.GetList(&#8220;<a href=\"http:\/\/sp01\/site\/Lists\/MyList\">http:\/\/sp01\/site\/Lists\/MyList<\/a>&#8220;)<br \/>PS &gt; $spList.OnQuickLaunch = $false<br \/>PS &gt; $spList.Update()<\/p>\n<p>* Enable folder creation on a list<\/p>\n<p>You can enable folder creation on a list in SharePoint 2010 using PowerShell. Simply store an instance of an SPList Object, set the EnableFolderCreation property to $true, and update the SPList object.<\/p>\n<p>PS &gt; $spWeb = Get-SPWeb <a href=\"http:\/\/sp01\/site\">http:\/\/sp01\/site<\/a><br \/>PS &gt; $spList = $spWeb.GetList(&#8220;<a href=\"http:\/\/sp01\/site\/Lists\/MyList\">http:\/\/sp01\/site\/Lists\/MyList<\/a>&#8220;)<br \/>PS &gt; $spList.EnableFolderCreation = $true<br \/>PS &gt; $spList.Update()<\/p>\n<p>* PowerShell commands for SharePoint 2010<\/p>\n<p>SOS Member santhiswaroop<\/p>\n<p>To find the PowerShell commands n SharePoint 2010, try these:<\/p>\n<p>&gt;Get-Command -Noun SPSite<\/p>\n<p>You can get the all PowerShell commands:<\/p>\n<p>&gt;Get-Help Get-SPSite<\/p>\n<p>You can get the Name, Syntax, Description, Related Links, Remarks<\/p>\n<p>To Retrieve content database for a specific site collection:<\/p>\n<p>&gt;Get-SPContentDatabase -Site <a href=\"http:\/\/SPServer01\">http:\/\/SPServer01<\/a><\/p>\n<p>Results:<\/p>\n<p>ID\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :96dfa345-43df-3edg-bbc6-1l4e8ee105le<br \/>Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :WSS_content<br \/>Web Application\u00a0\u00a0\u00a0\u00a0 :SPWeb Application Name=Sharepoint &#8211; 80<br \/>Server\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :SPServer01<br \/>CurrentSiteCount\u00a0\u00a0\u00a0 :2<\/p>\n<p>To retrieve the content database for a specific Web Application:<\/p>\n<p>&gt;Get-SPContentDatabase -WebApplication &#8220;Sharepoint &#8211; 80&#8221;<\/p>\n<p>Results:<\/p>\n<p>ID\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :96dfa345-43df-3edg-bbc6-1l4e8ee105le<br \/>Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :WSS_content<br \/>Web Application\u00a0\u00a0\u00a0\u00a0 :SPWeb Application Name=Sharepoint &#8211; 80<br \/>Server\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :SPServer01<br \/>CurrentSiteCount\u00a0\u00a0\u00a0 :2<\/p>\n<p>* Quickly Access the SharePoint Hive<\/p>\n<p>If you use SharePoint on a daily basis, you are probably familiar with the SharePoint hive. In SharePoint 2010, the hive is called the SharePoint root. The SharePoint root is used by SharePoint to store features, logs, binary files, templates and much more. It\u2019s commonly used when performing administrative tasks in SharePoint. Changing location to the path is fun, the first time, but after typing the path 100 times, it gets a bit tedious. In Windows PowerShell you can solve this by adding a new PSDrive that points to the SharePoint Root.<\/p>\n<p>New-PSDrive -Name SharePoint -Root &#8220;C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14&#8221; -PSProvider FileSystem<\/p>\n<p>If you place the command in your profile it will always be loaded when you start PowerShell.<\/p>\n<p>Now you can change location to the SharePoint Root using a simple command:<\/p>\n<p>cd SharePoint:<br \/>If you want to open the folder in explorer, simply type:<\/p>\n<p>explorer (Convert-Path SharePoint:)<\/p>\n<p>* Adding List Items using PowerShell<\/p>\n<p>SharePoint 2010 offers over 500 new cmdlets that we can use to automate the SharePoint 2010 environment through Windows PowerShell. The cmdlets go as deep as SPWeb, everything beyond SPWeb requires additional scripting, such as adding fields, views and items. Here\u2019s an example showing how to add an item to the Announcements list in SharePoint 2010:<\/p>\n<p>$spWeb = Get-SPWeb -identity <a href=\"http:\/\/SP\">http:\/\/SP<\/a><\/p>\n<p>$spList = $spWeb.GetList(\u201cLists\/Announcements&#8221;)<br \/>$newItem = $spList.AddItem()<br \/>$newItem[&#8220;Title&#8221;] = &#8220;My First Announcement&#8221;<br \/>$newItem[&#8220;Body&#8221;] = &#8220;&lt;h2&gt;PowerShell Magic&lt;\/h2&gt;&#8221;<br \/>$newItem[&#8220;Expires&#8221;] = &#8220;5\/5\/2010&#8221;<br \/>$newItem.Update()<br \/>$spWeb.Dispose()<\/p>\n<p>In the example we use the Get-SPWeb cmdlet to bind to a specific site. Next we use the GetList() method to retrieve a list. The method accepts a lists relative URL as input. We then use the AddItem() method to create a new item in SharePoint 2010. Finally we use the Dispose() method on the SPWeb object to make sure that the object is disposed of correctly.<\/p>\n<p>* Managing sites in SharePoint 2010 Using PowerShell cmdlets<\/p>\n<p>In SharePoint 2010 you can manage sites through Windows PowerShell. You can create new sites in a site collection using the New-SPWeb cmdlet:<\/p>\n<p>New-SPWeb <a href=\"http:\/\/SP\/TeamSite\">http:\/\/SP\/TeamSite<\/a> -Template &#8220;STS#0&#8221; \u2013Name &#8220;Team Site&#8221; `<br \/>-Description &#8220;Description of Site&#8221; \u2013AddToTopNav \u2013UseParentTopNav<\/p>\n<p>You can retrieve an existing site using the Get-SPWeb cmdlet:<\/p>\n<p>Get-SPWeb <a href=\"http:\/\/SP\/TeamSite\">http:\/\/SP\/TeamSite<\/a><\/p>\n<p>You can configure a site through the Set-SPWeb cmdlet:<\/p>\n<p>Get-SPWeb <a href=\"http:\/\/SP\/TeamSite\">http:\/\/SP\/TeamSite<\/a> | Set-SPWeb -Description &#8220;New Description&#8221;<\/p>\n<p>And finally, you can remove a site using the Remove-SPWeb cmdlet:<\/p>\n<p>Remove-SPWeb <a href=\"http:\/\/SP\/TeamSite\">http:\/\/SP\/TeamSite<\/a> -Confirm:$false<\/p>\n<p>Setting the \u2013Confirm switch parameter to $false omits the confirmation returned from PowerShell.<\/p>\n<p>Backup and Restore Site Collections in SharePoint 2010 using PowerShell<\/p>\n<p>Backup-SPSite <a href=\"http:\/\/SP\">http:\/\/SP<\/a> \u2013Path C:BackupsiteCollection.bak<\/p>\n<p>Restoring a site collection in SharePoint 2010 is just as simple:<\/p>\n<p>Restore-SPSite <a href=\"http:\/\/SP\">http:\/\/SP<\/a> \u2013Path C:BackupsiteCollection.bak<\/p>\n<p>* User Management in SharePoint 2010 with PowerShell<\/p>\n<p>SharePoint 2010 includes three cmdlets that you can use to manage users in SharePoint 2010. Here\u2019s an example on adding a new user:<\/p>\n<p>New-SPUser &#8220;XYZdavid&#8221; -Web <a href=\"http:\/\/SP\">http:\/\/SP<\/a> -PermissionLevel &#8220;Contribute&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>The example above adds the user \u201cdavid\u201d to the specified site and sets the user permissions to contribute.<\/p>\n<p>You can retrieve an existing user through the Get-SPUser cmdlet:<\/p>\n<p>Get-SPUser \u201cXYZdavid\u201d \u2013Web <a href=\"http:\/\/SP\">http:\/\/SP<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>If you pipe the object to the Format-List cmdlet you can list all properties.<\/p>\n<p>Get-SPUser \u201cXYZdavid\u201d \u2013Web <a href=\"http:\/\/SP\">http:\/\/SP<\/a> | Format-List *<\/p>\n<p>&nbsp;<\/p>\n<p>You can modify users through the Set-SPUser cmdlet:<\/p>\n<p>Set-SPUser \u201cXYZdavid\u201d -Web <a href=\"http:\/\/SP\">http:\/\/SP<\/a> -Email <a href=\"mailto:mail@XYZ.com\">mail@XYZ.com<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>And finally, you can remove a user through the Remove-SPUser cmdlet:<\/p>\n<p>Remove-SPUser \u201cXYZdavid\u201d -Web <a href=\"http:\/\/SP\">http:\/\/SP<\/a><\/p>\n<p>* Add Managed Paths to Web Applications using PowerShell<\/p>\n<p>You can use the New-SPManagedPath cmdlet to add managed paths to a given Web application:<\/p>\n<p>PS &gt; New-SPManagedPath -RelativeURL MyPath `<br \/>&gt;&gt; -WebApplication <a href=\"http:\/\/SP01.powershell.nu\">http:\/\/SP01.powershell.nu<\/a><\/p>\n<p>The example above creates a \u201cMyPath\u201d managed path for the Web application <a href=\"http:\/\/SP01.powershell.nu\">http:\/\/SP01.powershell.nu<\/a>.<\/p>\n<p>* Adding Alternate Access Mappings using PowerShell<\/p>\n<p>Adding Alternate Access Mappings is a simple task using Windows PowerShell. You can simply use the New-SPAlternateUrl cmdlet, which supports the creation of internal or public URLs for a given Web application. The examples below demonstrate how to create both a public and internal URL:<\/p>\n<p># Public<br \/>PS &gt; New-SPAlternateURL -Url <a href=\"http:\/\/public.powershell.nu\">http:\/\/public.powershell.nu<\/a> `<br \/>&gt;&gt; -Zone &#8220;Internet&#8221; -WebApplication <a href=\"http:\/\/sp01.powershell.nu\">http:\/\/sp01.powershell.nu<\/a><\/p>\n<p># Internal <br \/>PS &gt; New-SPAlternateURL -Url <a href=\"http:\/\/internal.powershell.nu\">http:\/\/internal.powershell.nu<\/a> `<br \/>-Zone Default -WebApplication <a href=\"http:\/\/sp01.powershell.nu\">http:\/\/sp01.powershell.nu<\/a> -Internal<\/p>\n<p>* Adding Managed Accounts using PowerShell<\/p>\n<p>You can add SharePoint 2010 managed accounts by using New-SPManagedAccount in Windows PowerShell. When creating automated scripts, you may want to check if an account is already a managed account. Simply use Get-SPManagedAccount and a simple if\/else statement to determine if the managed account already exists. Here\u2019s a short code example:<\/p>\n<p>$userName = &#8220;domainuser&#8221;<br \/>if(Get-SPManagedAccount | Where-Object { $_.UserName -eq $userName }){<br \/> # Managed Account Already exists<br \/> Write-Host \u201cManaged Account: $userName exists\u201d<br \/>} else {<br \/> # Get User Credentials<br \/> $credential = Get-Credential -Credential $userName<br \/> # Create New Managed Account<br \/> New-SPManagedAccount -Credential $credential<br \/>}<\/p>\n<p>* Adding Solutions<\/p>\n<p>Did you know that when you upload solution packages to a farm, you can use the Add-SPSolution cmdlet? Simply:<\/p>\n<p># Adding a Solution<br \/>Add-SPSolution \u2013LiteralPath C:StuffMySolution.wsp<br \/>After you\u2019ve uploaded a solution package, you can use the Install-SPSolution cmdlet. The example below will get you all solutions and then filter out the solution where the name is equal to MySolution.wsp. Finally, the solution is installed using the Install-SPSolution cmdlet.<\/p>\n<p>PS &gt; Get-SPSolution | Where-Object { $_.Name -eq &#8220;MySolution.wsp&#8221; } |<br \/>&gt;&gt; Install-SPSolution \u2013GACDeployment<\/p>\n<p>* Changing Custom Master Page<\/p>\n<p>You can use Windows PowerShell to change a Web\u2019s custom master page. Simply point the CustomMasterUrl to a new master page:<\/p>\n<p>PS &gt; $spWeb = Get-SPWeb <a href=\"http:\/\/SP01.powershell.nu\">http:\/\/SP01.powershell.nu<\/a><br \/>PS &gt; $spWeb.CustomMasterUrl = &#8220;\/_catalogs\/masterpage\/myNew.master&#8221;<br \/>PS &gt; $spWeb.Update()<br \/>PS &gt; $spWeb.Dispose()<\/p>\n<p>* Create Default Associated Groups<\/p>\n<p>When creating Web site using PowerShell, you may encounter situations where you\u2019re missing the site\u2019s default groups. A simple solution to this is using the CreateDefaultAssociatedGroups method. The example below demonstrates how you can use the method:<\/p>\n<p>PS &gt; $spWeb = Get-SPWeb <a href=\"http:\/\/SP01.powershell.nu\">http:\/\/SP01.powershell.nu<\/a><br \/>PS &gt; $spWeb.CreateDefaultAssociatedGroups(&#8220;powershellspAdmin&#8221;,&#8221;&#8221;,&#8221;&#8221;)<br \/>PS &gt; $spWeb.Update()<br \/>PS &gt; $spWeb.Dispose()<\/p>\n<p>* Changing Locale<\/p>\n<p>You can change a Web site\u2019s locale setting using Windows PowerShell. Simply replace the value of the locale property by using an object of the type System.Globalization.CultureInfo. The example below shows how to set the locale to Swedish:<\/p>\n<p>PS &gt; $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture(\u201csv-SE\u201d)<br \/>PS &gt; $spWeb = Get-SPWeb <a href=\"http:\/\/SP01.powershell.nu\">http:\/\/SP01.powershell.nu<\/a><br \/>PS &gt; $spWeb.Locale = $culture <br \/>PS &gt; $spWeb.Update()<br \/>PS &gt; $spWeb.Dispose()<\/p>\n<p>* Enable Auditing<\/p>\n<p>You can enable site audit using Windows PowerShell. The example below demonstrates how to add specific audit flags on a site collection and enable auditing:<\/p>\n<p>PS &gt; $spSite = Get-SPSite <a href=\"http:\/\/SP01.powershell.nu\">http:\/\/SP01.powershell.nu<\/a><br \/>PS &gt; $spSite.Audit.AuditFlags = <br \/>&gt;&gt; ([Microsoft.SharePoint.SPAuditMaskType]::All `<br \/>&gt;&gt; -bxor[Microsoft.SharePoint.SPAuditMaskType]::View)<br \/>PS &gt; $spSite.Audit.Update()<br \/>PS &gt; $spSite.Dispose()<\/p>\n<p>* Handling Errors in Your Scripts<br \/>When creating automated SharePoint scripts using Windows PowerShell, it\u2019s a good idea to add some form of error handling. A typical example is when you try to access a non-existent site collection. By default, Windows PowerShell displays an error message. You can use a Try\/Catch statement if you want to handle this. Note that the \u2013ErrorAction is set to stop on the cmdlet:<\/p>\n<p>$url = &#8220;<a href=\"http:\/\/SP01.powershell.nu\">http:\/\/SP01.powershell.nu<\/a>&#8220;<br \/>try {<br \/> # Trying to get the Site Collection<br \/> # ErrorAction is set to Stop<br \/> Get-SPSite $url -ErrorAction Stop<br \/>}<br \/>catch {<br \/> # If the Command in the Try block fails,<br \/> # The code in the Catch block is executed<br \/> Write-Host \u201cThat didn\u2019t work..\u201d<br \/>}<\/p>\n<p>* Removing Nodes from the QuickLaunch<\/p>\n<p>You can use Windows PowerShell when removing one (or more) nodes from a Web site\u2019s QuickLaunch. The example below shows\u00a0 how you can remove everything except for nodes under the lists heading:<\/p>\n<p>PS &gt; $spWeb = Get-SPWeb <a href=\"http:\/\/SP01.powershell.nu\">http:\/\/SP01.powershell.nu<\/a> -ErrorAction Stop<br \/>PS &gt; foreach($node in ($spWeb.Navigation.QuickLaunch | <br \/>&gt;&gt; Where-Object { $_.Title -ne &#8220;Lists&#8221; })) {<br \/>&gt;&gt; $node.Delete()<br \/>&gt;&gt; }<br \/>PS &gt; $spWeb.Dispose()<\/p>\n<p>* Remove Group Permissions<br \/>You can remove a group\u2019s permissions on a specific Web site with\u00a0 Windows PowerShell. Simply remove the group from the role assignment collection:<\/p>\n<p>PS &gt; $spWeb = Get-SPWeb <a href=\"http:\/\/SP01.powershell.nu\">http:\/\/SP01.powershell.nu<\/a><br \/>PS &gt; $spPrincipal = [Microsoft.SharePoint.SPPrincipal]$spWeb.SiteGroups[&#8220;Site Members&#8221;]<br \/>PS &gt; $spWeb.RoleAssignments.Remove($spPrincipal)<br \/>PS &gt; $spWeb.Dispose()<\/p>\n<p>Source:<br \/><a href=\"http:\/\/secretsofsharepoint.com\/cs\/blogs\/tips\/default.aspx\">http:\/\/secretsofsharepoint.com\/cs\/blogs\/tips\/default.aspx<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>*Get local farm information using PowerShellUsing\u00a0 the Get-SPFarm cmdlet, you can retrieve information, including\u00a0 the name of the Config database, version number, and servers in the farm\u00a0 that can be useful when creating scripts for your SharePoint farm. You can open up the SharePoint 2010 Management Shell on your SharePoint server to view the settings [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,1],"tags":[],"_links":{"self":[{"href":"http:\/\/obieta.com\/index.php?rest_route=\/wp\/v2\/posts\/1548"}],"collection":[{"href":"http:\/\/obieta.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/obieta.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/obieta.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/obieta.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1548"}],"version-history":[{"count":0,"href":"http:\/\/obieta.com\/index.php?rest_route=\/wp\/v2\/posts\/1548\/revisions"}],"wp:attachment":[{"href":"http:\/\/obieta.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/obieta.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1548"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/obieta.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}