SharePoint Administration with PowerShell (part 1)

2/8/2011 9:00:25 AM

The Naming of PowerShell Cmdlets

The naming of PowerShell cmdlets differs from the ones you got used to in the SharePoint Central Administration interface and is much similar to STSADM and API naming. Here are the most commonly used cmdlets:

  • Get-WebApplication— Returns all SharePoint web applications

  • Get-SPSite— Returns all site collections

  • Get-SPWeb— Returns all SharePoint sites in a given site collection

Retrieving Site Collections and Sites with PowerShell

One of the most common scenarios when you use PowerShell is retrieving sites collections and sites. To retrieve the list of site collections, simply type Get-SPSite in the Shell.

To retrieve the list of all SharePoint sites in a site collection, Get-SPSite needs to be combined with Get-SPWeb. For example, type the following:

Get-SPSite | Get-SPWeb

This command lists all site collections in a SharePoint farm and then lists all the websites in each site collection.

In case you need to list format or all object properties, type

Get-SPSite | Select *

Your PowerShell shell lists all the site collections with all the associated properties.

Modifying Site Collection Properties with PowerShell

Administrators can use PowerShell to easily modify all the properties of any PowerShell object.

Let’s consider the following scenario. An administrator needs to add user CompanyABC.com\John as a secondary administrator for all site collections. This is an ideal example of how PowerShell can help to automate SharePoint administration. To achieve the goal, type the following command:

Get-SPSite | Set-SPSite –SecondaryOwnerAlias "CompanyABC.com\John"

In the preceding example, we use Get-SPSite to get the list of all the site collections and then combine that with the Set-SPSite cmdlet that sets a Secondary Owner value to “CompanyABC.com\John”.

To verify that the cmdlet completed successfully, run the following command that will display the owner and secondary owner for each site collection:

Get-SPSite | Select RootWeb, Url, Owner, SecondaryContact

Working with Solutions and Features

One of the most frequent tasks that administrators perform on SharePoint farms is solution and feature management. Previously, in SharePoint 2007, administrators couldn’t install solutions without calling STSADM commands. In SharePoint 2010, this has not changed much; you still can use STSADM to install custom solution, but there are dedicated PowerShell cmdlets that can help you further automate this procedure.

Deploying a Custom Solution with Features

Let’s consider the following scenario: A custom solution needs to be deployed to CompanyABC SharePoint Portal. The solution has two features: a content type and custom web part, as shown in Figure 1.

Figure 1. The structure of custom ABC-Solution. It contains two features: a web part HelloWorld and a custom ContentType.

Here is the series of steps you need to do to install the ABC feature to a SharePoint environment.

The first step of this procedure is to install the solution from the file system to central solution farm store. Use this command (use the path where you have a .wsp file):

Add-SPSolution "C:\My-Solutions\ABC-Solution.wsp"

When the command installs the solution, it will be visible in the Central Administration, Solution Management. You need to deploy the solution via the central administration user interface or with the following PowerShell code:

Install-SPSolution "CompanyABC_Solution.wsp" -AllWebApplications -GACDeployment -Force

The preceding command deploys solutions to all web applications (-AllWebApplications parameter) and also enables custom code to be deployed to Global Assembly Cache (GAC) via the –GACDeployment parameter. If the solution already exists, it will be overwritten (-Force).

To activate features on individual site collection, use the following code. The ABC solution has two features, and each needs to be enabled individually:

Enable-SPFeature "ABC-Solution_HelloWorld" -Url "http://portal.companyABC.com"
Enable-SPFeature "ABC-Solution_ContentType" -Url "http://portal.companyABC.com"

In the custom ABC-Solution, the HelloWorld web part is scoped as a site collection (Site) feature, and the ContentType is scoped as a site-level (web) feature. To activate it on every site in your site collection, run the following command:

Get-SPSite "http://portal.companyABC.com" | Get-SPWeb | ForEach-Object {Enable-SPFeature "ABC-Solution_ContentType" -Url $_.Url}

To remove these features and solution, you must call commands in reverse order: First deactivate the feature, uninstall the solution, and then remove it. To perform these operations, type the following:

Disable-SPFeature "ABC-Solution_HelloWorld" -Url "http://portal.companyABC.com" -confirm:$false
Disable-SPFeature "CompanyABC-Solution_ContentType" -Url "http://portal.companyABC.com" -confirm:$false

Uninstall-SPSolution "ABC-Solution.wsp" -confirm:$false -AllWebApplications:$true
Remove-SPSolution "ABC-Solution.wsp" -confirm:$false

  •  Sharepoint 2007: Approve or Reject a File or List Item
  •  Exchange Server 2007 : Configure the Client Access Server - Enable POP3 and IMAP4
  •  Exchange Server 2007 : Configure the Client Access Server - Enable and Configure Outlook Anywhere
  •  Exchange Server 2007 : Configure the Client Access Server - Create and Apply ActiveSync Mailbox Policies
  •  SharePoint 2010 : Understanding Windows PowerShell Concepts (part 3)
  •  SharePoint 2010 : Understanding Windows PowerShell Concepts (part 2)
  •  SharePoint 2010 : Understanding Windows PowerShell Concepts (part 1)
  •  Managing and Administering SharePoint 2010 Infrastructure : Using Additional Administration Tools for SharePoint
  •  Managing Exchange Server 2010 : Archiving and compliancy (part 3) - Discovery
  •  Managing Exchange Server 2010 : Archiving and compliancy (part 2) - Messaging Records Management
    Most View
    The Cat You Have To Have (Part 3)
    Acer Aspire S7-391 Ultrabook
    A Trio From HIS: 7970 IceQ X² GHz Edition, 7950 IceQ X² Boost Clock And 7850 IceQ Turbo X Graphics Cards Review (Part 4)
    Manage and Covert Your eBooks (Part 2)
    Armaggeddon Mikoyan Foxbat, Shteelseries Sensei Major League Gaming Edition, Logitech G600
    DIY - Make Your Own CMS (Part 2)
    Kyocera Torque - An Ultra-Durable Phone Providing Prominent Sound (Part 1)
    Toshiba Satellite U925t Review (Part 1)
    Sharepoint 2013 : Upload a File (part 2) - Upload a File from an Office Application, Upload a File Using Web Folders
    Patriot Memory Gauntlet Node
    Top 10
    Tecdesk Smart 5500 (Part 2)
    Tecdesk Smart 5500 (Part 1)
    Thunderstruck ASUS Brings Thunderbolt 2 To Your PC (Part 3)
    Thunderstruck ASUS Brings Thunderbolt 2 To Your PC (Part 2)
    Thunderstruck ASUS Brings Thunderbolt 2 To Your PC (Part 1)
    Toshiba BDX5400KB Smart 3D Bluray Player With Built-in Wifi
    Samsung BD-F7500 Smart Bluray Player
    Samsung Galaxy Note 10.1 2014 Edition
    Samsung Galaxy NotePro 12.2 (Part 2)
    Samsung Galaxy NotePro 12.2 (Part 1)