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:
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
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.
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