PowerShell in SharePoint 2010

 

1) What is the SharePoint 2010 management shell ?

2) Manage your SharePoint Server using PowerShell

2.1 Refresh Installed components

2.2 Change the port of the Central Administration site

2.3 Change your farm's security passphrase

2.4 Find what are your farm's managed accounts

2.5 Rename your SharePoint server

2.6 Get information on SharePoint databases

2.7 Get information on a Specific database

2.8 Create a new content database

2.9 Delete a new content database

2.10 Create a new configuration database

2.11 Delete a configuration database

2.12 Backup and restore the configuration database

2.13 Backup and restore your Sharepoint farm

2.14 Backup and restore a site collection

3) Web Application Management / Site Management

3.1 Get a web application using PowerShell

3.2 Remove a web application using PowerShell

3.3 Create a new web application using PowerShell

3.4 Set parameters for a web application

3.5 Extending a web application using PowerShell

3.6 Alternate access mappings

3.7 Gather information about site collections

3.8 Create a site collection

3.9 Remove a site collection

3.10 Subsite management

3.11 Site collections and content databases

4) Manage Service Applications

4.1 Display available service applications

4.2 Get info about a specific service application

4.3 Publish a service application to another farm

4.4 Delete a service application

5) SharePoint solutions/features and PowerShell

5.1 Solution/Feature management

5.1.1 Exporting Farm Solutions

6) Enterprise Search

6.1 Get Service information

6. 2 Get Search Service instances

6.3 Get information about the Search Service Application

6.4 Get information about the available Metadata categories

6.5 Get information about the available Managed Properties

 


 

1) What is the SharePoint 2010 management shell ?

The SharePoint 2010 Management Shell is an extension of  Windows PowerShell created for  the administration and management of SharePoint. Once SharePoint is installed on the server, the management console is available from the Start menu

MGMTConsole

After running the management console, you should see a window that looks like:

MGMTShell1

Hint: you can also load the SharePoint extensions (snap-ins) from a Windows PowerShell by using the command Add-PsSnapin Microsoft.SharePoint.PowerShell

TOP of Page


2) Manage your SharePoint Server using PowerShell

2.1) Refresh installed components

In order to ensure that your SharePoint installation was successfully, you can issue the following cmdlet to ensure that the SharePoint products installed on a farm server are properly registered:
Set-SPFarmConfig -InstalledProductsRefresh

2.2) Change the port of the Central Administration site

Use the Set-SPCentralAdministration cmdlet to change the port number configured for the Central Administration web application.

 

2.3) Change your farm's security passphrase

Use the Set-SPPassPhrase cmdlet is solely used to change the SharePoint farm passphrase. This passphrase was initially entered during the installation of the SharePoint farm.

passphrase

2.4) Find what are your farm's managed accounts

Executing  the Get-SPManagedAccount cmdlet displays all the managed accounts on the SharePoint farm.

MGAccnt

A Managed Account is effectively an Active Directory user account whose credentials are managed by and contained within SharePoint. In addition to storing the credentials of the object, Microsoft SharePoint Server 2010 can also leverage Active Directory Domain Policies to automatically reset passwords while meeting the requirements established by policy.

2.5) Rename your SharePoint server

The Rename-SPServer cmdlet is used to rename a server in the farm. This only renames the server from a SharePoint standpoint and does not actually rename the physical server.

Example: Rename-SPServer –Identity "OldName" –Name "NewName"

 

2.6) Get information on SharePoint databases

 Use the Get-SPDatabase cmdlet with the ServerInstance parameter to retrieve all available databases.

DB1

Note: if you are using an instance, other than the default one, use: Get-SPDatabase –ServerInstance "SQL-PBNET\InstanceName"

 

2.7) Get information on a Specific database

Use the Get-SPDatabase cmdlet using the –Identity parameter to assign a variable the results.

DB2

 

2.8) Create a new content database

The New-SPContentDatabase cmdlet allows you to create a new content database using the Name parameter for the name and the WebApplication parameter to associate it to the specified web application.

 

    2.9) Delete a new content database

The Remove-SPContentDatabase cmdlet allows you to delete the specified content database

DB3

    2.10) Create a new configuration database

Use the New-SPConfigurationDatabase cmdlet to create a new configuration database and associate it to the specified web application.
The New-SPConfigurationDatabase cmdlet allows you to create a new configuration database using the DatabaseName and DatabaseServer parameters.
Example: New-SPConfigurationDatabase –DatabaseName NewConfigurationDB–DatabaseServer "SQL-PBNET\InstanceName"

Note1: The farm credentials and a passphrase are needed to execute this cmdlet.
Note2: If the current server is already joined to an existing farm, you will not be able to create a new configuration database. Use the Disconnect-SPConfigurationDatabase first.

 

2.11) Delete a configuration database

Use the Remove-SPConfigurationDatabase cmdlet  to delete the current configuration database.
Example: Remove-SPConfigurationDatabase
Note: The cmdlet already knows which configuration database is being used by the current server and SharePoint farm.

 

2.12) Backup and restore the configuration database

Use the Backup-SPConfigurationDatabase cmdlet  to perform a backup on a configuration database, placing the backup files in a specific folder.
DB4


Note: If you are backing up the current configuration database being used by the farm, you may also use the Backup-SPFarm cmdlet with the ConfigurationOnly switch parameter,
Backup-SPFarm –BackupMethod Full –Directory F:\Backups -ConfigurationOnly
Note2:  For backup and restore operations of the current farm, it is recommended to use the Backup-SPFarm cmdlet with the ConfigurationOnly switch parameter to
back up the configuration database.
Note3: The BackupMethod parameter can be either Full or Differential . Full will back up the entire configuration, whereas Differential only backs up changes that have occurred since the last full backup was performed.

To restore the configuration database use: Restore-SPFarm –Directory F:\Backups –ConfigurationOnly –RestoreMethod Overwrite

2.13) Backup and restore your Sharepoint farm

The Backup-SPFarm cmdlet allows you to perform a backup of the entire farm (or item in the farm), placing the backup files in the specified folder.
Example: Backup-SPFarm –BackupMethod Full –Directory C:\Backups
The BackupMethod parameter can be either Full or Differential . Full will back up the entire configuration, whereas Differential only backs up changes that have occurred since the last full backup was performed.
Note: Run Backup-SPFarm with the –ShowTree parameter to display all items in the farm that may be backed up.
Use the –Item parameter with Backup-SPFarm to back up a specific item in the farm.

The Restore-SPFarm cmdlet allows you to restore the farm based on the backup files in the specified folder.
Example: Restore-SPFarm –Directory F:\Backups –RestoreMethod Overwrite
The RestoreMethod parameter can be New or Overwrite . Using New creates a new location for the backup to be restored. Overwrite will restore the backup in place and overwrite the current farm content.
Note: Use the –Item parameter with Restore-SPFarm cmdlet to restore a specific item in the farm.

 

2.14) Backup and restore a site collection

The Backup-SPSite cmdlet allows you to perform a backup of a specific site collection, creating a backup file based on the path specified.
Example: Backup-SPSite –Identity "http://spspbnet/sites/testsite" –Path C:\Backups\testsite.bak
The Identity parameter can be the URL or the GUID of the site collection. Using the URL is usually easier. The Path parameter defines where the backup will be stored, along with the name of the backup file to create.
Note: You can run Get-SPSite without any parameters to list out the available site collections.

The Restore-SPSite cmdlet allows you to restore a site collection based on the backup file in the path specified.
Example: Restore-SPSite –Identity "http://spspbnet/sites/testsite" –Path C:\Backups\testsite.bak –Force
The Force switch parameter is needed to overwrite an existing site collection. You may also specify the DatabaseName and DatabaseServer parameters to specify where the content of the site collection should be stored.

   

TOP of Page


3) Web Application Management / Site Management

3.1) Get a web application using PowerShell

Use the Get-SPWebApplication cmdlet without any parameters to retrieve all available web applications.

webapp1

Note: to get info about a specific web application use: Get-SPWebApplication "Application Name"

 

3.2) Remove a web application using PowerShell

Use the Remove-SPWebApplication cmdlet allows you to remove a specific web application from the current server.
You should  use this cmdlet in conjunction with the Get-SPWebApplication cmdlet (see section 3.1) or just use the web application identity directly in line: Remove-SPWebApplication –Identity "WebApp"
Example:  Removing a Web Application Using a Variable
$webApp = Get-SPWebApplication -Identity "WebApp"
Remove-SPWebApplication $webApp


Note:  You may also remove the underlying database(s) and data by using the -RemoveContentDatabases switch parameter. Only use this if you are no longer using the web application at all on the farm.

    webapp2

3.3) Create a new web application using PowerShell

Use the New-SPWebApplication cmdlet in order to create a new web application as follows:
New-SPWebApplication –applicationpool <name of new application pool> -name <name of new web app> -applicationpoolaccount <service account to be used for the application pool> -port <desired port number>

LISTING 5.5 Creating a New Web Application
New-SPWebApplication –applicationpool NewWebApp -name NewWebApp
-applicationpoolaccount SP\AppPoolAccount -port 80


    Webapp3

3.4) Set parameters for a web application

Use the Set-SPWebApplication cmdlet has several parameter sets for configuring various settings on the web application.
Note:  Running Set-SPWebApplication with the -? parameter displays each parameter set.

3.5) Extending a web application using PowerShell

If you need to extend a web application to a different zone, that zone must not already be in use.
Use the New-SPWebApplicationExtension cmdlet. The cmdlet only requires the web application identity, the name of the new website that will extend the web application,
and the zone. However, it is a good idea to include the new URL and host header that the web application extension will use for the zone.
Example: New-SPWebApplicationExtension –Identity "SharePoint – 80" –Name "SP Internet Site" –Zone Internet –Url "http://internet" –HostHeader "http://internet.corp.pbnet.com"
The URL is the public-facing HTTP address, whereas the host header entry is the internal alternate access mapping. The Zone parameter may be one of the following values:
- Default
- Intranet
 - Internet
 - Extranet
 - Custom
The zone used in the command must not already be used. There can’t already be a web application extended to that zone.

3.6) Alternate access mappings

You can create alternate access mappings using the New-SPAlternateURL cmdlet.
Example: New-SPAlternateURL –URL "http://internet.corp.pbnet.com" –WebApplication "SharePoint - 80" –Internal –Zone Intranet
Using the –Internal switch parameter creates the mapping for internal use.
Omitting this parameter generates a public URL entry.
Use the –Zone parameter to specify the zone. Here are the valid values:
- Default
- Intranet
- Internet
- Extranet
- Custom

Display All Alternate Access Mappings

Use the Get-SPAlternateURL cmdlet without any parameters.
Note: To get a Specific Alternate URL Entry: use the Get-SPAlternateURL cmdlet using the –Identity parameter and assign a variable the results.
$alternateURL = Get-SPAlternateURL -Identity "http://internet.corp.pbnet.com"

Change the Zone of an Alternate Access Mapping

When you need to change the zone of an existing alternate access mapping, use the Set-SPAlternateURL cmdlet allows you to modify the zone. This is the only
setting that may be modified using the cmdlet. The –Zone parameter may be one of the following values:
- Default
- Intranet
- Internet
- Extranet
- Custom
Example: Set-SPAlternateURL –Identity "http://internet.corp.pbnet.com" –Zone Intranet
Note: The –Identity parameter can be the incoming URL or a variable identifying the alternate access mapping (using the Get-SPAlternateURL cmdlet).
Note: If the alternate access mapping entry is public and the zone is Default , you may not change the zone until another public entry is created for the default zone.

Remove an Alternate Access Mapping


When you need to remove an existing alternate access mapping entry, use the Remove-SPAlternateURL cmdlet allows you to remove the specified alternate URL entry.
The only required parameter is the identity of the URL mapping
Example: Remove-SPAlternateURL –Identity "http://internet.corp.pbnet.com"
Note: If the alternate access mapping entry is public and the zone is Default , you may not remove the alternate URL entry.

 

    3.7) Gather information about site collections

Using the Get-SPSite cmdlet with the WebApplication parameter,  displays all available site collections in the specified web application.
Example: Get-SPSite –WebApplication "SharePoint – 80"

SPSite1

Display Available Site Collections in a Content Database

Use the Get-SPSite cmdlet with the ContentDatabase parameter.
Example: Get-SPSite –ContentDatabase "WSS_CONTENT1"
Note: You can use  the SP-GetContentDatabase cmdlet to display the available content databases used by the SharePoint farm.

Get a Specific Site Collection
Use the Get-SPSite cmdlet to assign a variable the results using the – Identity parameter.
Providing the identity of the site collection with the Get-SPSite cmdlet retrieves a specific site collection.
Use this to assign the results to an object for use in other cmdlets, as shown below:
$webApp = Get-SPSite -Identity "http://sps-pbnet"

 

3.8) Create a site collection

To create a new site collection, use the New-SPSite cmdlet as follows:
Example: New-SPSite –url <full url of new site collection> -name <name of new site collection> -owneralias <site collection administrator> -template <site collection template to use>
New-SPSite –url http://sps-pbnet:9191/sites/sitecol -name Home -owneralias CORP\Administrator -template STS#0

SPSite2

Note:
The site template names can be found using Get-SPWebTemplates .

templates1

If no template is provided, the site collection will still be created. In this case, once the site is accessed via the browser, SharePoint will prompt for a site collection template to be selected.

    3.9) Remove a site collection

Use the Remove-SPSite cmdlet allows you to remove a specific site collection from the current server.
Note: Use this in conjunction with the Get-SPSite cmdlet
Example:
$siteCol = Get-SPSite -Identity "http://sps-pbnet/sites/sitecol"
Remove-SPSite $siteCol
Example: Remove-SPSite –Identity "http://sps-pbnet/sites/sitecol"
Note: The Remove-SPSite cmdlet has an optional switch parameter named GradualDelete . Using this parameter will remove the site collection and any subsites underneath gradually.

SpSite3

    3.10) Subsite management

Get a Specific Subsite (Web)
When you need to obtain a specific subsite reference, use the Get-SPWeb cmdlet to assign a variable the results using the –Identity parameter.
Providing the identity of the subsite with the Get-SPWeb cmdlet retrieves a specific subsite.LISTING 8.13 Assigning a Variable to a Specific Subsite
Example: $web = Get-SPWeb -Identity "http://sps-pbnet/sites/sitecol/subsite"

Remove a Subsite (Web) from a Site Collection
When you need to remove a subsite from a site collection, use the Remove-SPWeb cmdlet.
The Remove-SPWeb cmdlet allows you to remove a specific subsite. Use this in conjunction with the Get-SPWeb cmdlet
Example:
$web = Get-SPWeb -Identity "http://sps-pbnet/sites/sitecol/subsite"
Remove-SPWeb $web
Example:
Remove-SPWeb –Identity "http://sps-pbnet/sites/sitecol/subsite"

Modify the Subsite (Web) URL
When you want to rename the relative URL name for a subsite, use the Set-SPWeb cmdlet with the RelativeURL parameter.
Example: Set-SPWeb –Identity "http://sps-pbnet/sites/sitecol/newsubsite" –RelativeURL teamsites
Note:  The example is essentially renaming the subsite URL from “newsubsite” to “teamsites.” Therefore, after execution the subsite is accessed by http://sps-pbnet/sites/sitecol/teamsites/.

3.11)  Site collections and content databases


Move a Site Collection to a Different Content Database
When you want to move a particular site collection to a different content database, use the Move-SPSite cmdlet.
The Move-SPSite cmdlet allows you to easily move a site collection from one content database to another.
Example: Move-SPSite –Identity "http://sps-pbnet/sites/sitecol" –DestinationDatabase "WSS_CONTENT_TEAMSITES"

Move All Site Collections from One Content Database to Another
You should use the Move-SPSite cmdlet with the Get-SPSite cmdlet.
Example:
Get-SPSite –ContentDatabase "WSS_CONTENT_MAIN" | Move-SPSite –DestinationDatabase "WSS_CONTENT_TEAMSITES"

 

TOP of Page


 

4) Manage Service Applications

 

    4.1) Display available service applications

Use the Get-SPServiceApplication cmdlet without any parameters to retrieve all available service applications.

ServiceApps

 

4.2) Get info about a specific service application

Use the Get-SPServiceApplication cmdlet to assign a variable the results using the –Identity parameter.
Providing the identity of the service application with the Get-SPServiceApplication cmdlet retrieves a specific service application.
$serviceapp = Get-SPServiceApplication -Identity 0e28171b-c80d-4af3-8c58-9f2126e0fda4
Note: Instead of using –Identity , you may use –Name instead and provide the name of the service application in quotes.

GetSerAppPS

 

4.3) Publish a service application to another farm

In Microsoft SharePoint Server 2010, some service applications can be published across farms and thereby be made available over remote connections. This article describes how to publish a service application.

Use the Publish-SPServiceApplication cmdlet with a service application variable.
The Publish-SPServiceApplication cmdlet allows you to make a specific service application available outside of the current farm. Use this in conjunction with the Get-SPServiceApplication cmdlet (see 4.2).

Note: To stop sharing a service application, use UnPublish- SPServiceApplication .

For more info, please consult the official TechNet article at: http://technet.microsoft.com/en-us/library/ee704545(v=office.14).aspx - Publish a service application (SharePoint Server 2010)

publishsa

    4.4) Delete a service application

Use the Remove-SPServiceApplication cmdlet with a service application variable.
This allows you to remove a specific service application from the current server. Use this in conjunction with the Get-SPServiceApplication cmdlet like in the example below:
$serviceApp = Get-SPServiceApplication -Identity 0e28171b-c80d-4af3-8c58-9f2126e0fda4
Remove-SPServiceApplication $serviceApp

Note:
You may also remove the underlying database and data by using the –RemoveData switch parameter. Only use this if you are no longer using the service application at all on the farm.

DelServiceApp

 

TOP of Page


5) SharePoint solutions/features and PowerShell

    5.1a) Solution  management

5.1b) Feature Management

5.1.1 Export Installed Farm Solutions

foreach($solution in Get-SPSolution)
{
    try
        {
        $filename = $solution.Name;
        $solution.SolutionFile.SaveAs(“C:\MySolutions\$filename”)
        }
    catch
        {
        Write-Host “-error:$_”-foreground red
        }
}

 

TOP of Page


 

6) Enterprise Search

6.1 Get Service information

 Use the Get-SPEnterpriseSearchService cmdlet provides information on the Enterprise Search Service that is running on the SharePoint farm.

GetENTSearch

6. 2 Get Search Service instances

Use the Get-SPEnterpriseSearchServiceInstance cmdlet to get information on the Enterprise Search Service instances that are mapped to the search service

SearchInstance

6.3 Get information about the Search Service Application

Use the Get-SPEnterpriseSearchServiceApplication cmdlet to get information on the Enterprise Search Service applications configured on the SharePoint farm

    getssa

6.4 Get information about the available Metadata categories

Use the Get-SPEnterpriseSearchMetadataCategory cmdlet to get information on the Enterprise Search metadata categories configured for a particular search service application

Example:
PS C:\Users\Administrator.CORP> $ssApp = Get-SPEnterpriseSearchServiceApplication -Identity 7e76645e-8f3d-45ea-bb0e-157ea941f49d
PS C:\Users\Administrator.CORP> Get-SPEnterpriseSearchMetadataCategory -SearchApplication $ssApp

metadata1

6.5 Get information about the available Managed Properties

Use the Get-SPEnterpriseSearchMetadataManagedProperty cmdlet provides to get information on the Enterprise Search metadata categories configured for a particular search service application.

    Example:
$ssApp = Get-SPEnterpriseSearchServiceApplication -Identity 7e76645e-8f3d-45ea-bb0e-157ea941f49d
Get-SPEnterpriseSearchMetadataManagedProperty –SearchApplication $ssApp

The results of the PowerShell cmdlets will look like:

ManagedProperties

 

TOP of Page