Let’s see which cmdlets are provided for handling site collections:
PS > Get-Command -Noun SPSite
CommandType Name Definition
----------- ---- ----------
Cmdlet Backup-SPSite Backup-SPSite [-Identity] <SPSitePipeBind>
Cmdlet Get-SPSite Get-SPSite [-Limit <String>] [-WebApplicat
Cmdlet Move-SPSite Move-SPSite [-Identity] <SPSitePipeBind> -
Cmdlet New-SPSite New-SPSite [-Url] <String> [-Language <UIn
Cmdlet Remove-SPSite Remove-SPSite [-Identity] <SPSitePipeBind>
Cmdlet Restore-SPSite Restore-SPSite [-Identity] <String> -Path
Cmdlet Set-SPSite Set-SPSite [-Identity] <SPSitePipeBind> [-
We’ll start with the Set-SPSite cmdlet.
Configuring a Site Collection in SharePoint 2010
The Set-SPSite cmdlet is used to configure a
site collection. This cmdlet supports a few interesting parameters that
you can use to change a site collection. Here’s how to add a secondary
owner to a site collection:
PS > Set-SPSite -Identity http://SPServer01 -SecondaryOwnerAlias powershell\nigo
In this example, we use the SecondaryOwnerAlias parameter and set the domain user powershell\nigo as the secondary owner of the site collection.
Another nice feature is the UserAcountDirectoryPath
parameter, which defines a scope for user accounts, meaning that only
accounts within the organizational unit can be added as members of the
site collection. People pickers will also be limited to this scope. The
following example limits the scope to the Company/Site/Users
organizational unit.
PS > Set-SPSite -Identity http://SPServer01 '
>> -UserAccountDirectoryPath "OU=Users,OU=Site,OU=Company,DC=Powershell,DC=nu"
If we now try searching for users through the people
picker in SharePoint, we will be able to find users only within the
scope and users already added to the site collection.
Note
Users added before the scope change is committed will still be able to access the site collection.
Backing Up and Restoring Site Collections in SharePoint 2010
You can take a backup of a site collection with the Backup-SPSite cmdlet and restore a site collection from a backup file using the Restore-SPSite cmdlet. The use of these cmdlets is pretty straightforward.
Here is an example of taking a backup of a site collection:
PS > Backup-SPSite -Identity http://SPServer01 -Path C:\Backup\siteCollection.bak
By default, the site collection is temporarily set to
read-only, so that no changes can be made while the backup is
performed. The Backup-SPSite cmdlet also supports the NoSiteLock
switch parameter, which specifies that the site collection not be
locked during the backup, however, this parameter is not recommended
using if users are writing to the site collection while a backup is
performed. Using the UseSqlSnapshot parameter is recommended if the database server hosting the content database supports database snapshots.
After you have a backup file of the site collection, you can use the Restore-SPSite cmdlet to restore the site collection:
PS > Restore-SPSite -Identity http://SPServer01 -Path C:\Backup\siteCollection.bak
Confirm
Are you sure you want to perform this action?
Performing operation "Restore-SPSite" on Target "http://SPServer01".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"): Y
Typing Y or pressing ENTER at the confirmation prompt performs the action and restores the site collection.
Creating a New Site Collection
To create new site collection, use the New-SPSite cmdlet. This cmdlet has two required parameters: Url and OwnerAlias. You can specify a template with the Template parameter. It is also possible to specify a content database to use with the ContentDatabase parameter.
Here is an example of creating a new site collection:
PS > New-SPSite -Url http://SPServer01:5077 '
>> -OwnerAlias powershell\administrator -Template "STS#0"
Url
---
http://spserver01:5077
Here, we point the URL to the root of an existing web
application, set the owner for the site collection, and specify a
template. The template name might look a little strange, but if you take
a quick peek with the Get-SPWebTemplate cmdlet, you will see which template we are using.
Removing Site Collections in SharePoint 2010
The Remove-SPSite cmdlet completely deletes an existing site collection and all sites. The cmdlet supports the GradualDelete
switch parameter, which removes the site collection gradually, reducing
system load. This parameter is recommended for deleting large sites.
PS > Remove-SPSite -Identity http://SPServer01:5077 -GradualDelete -Confirm:$False
In this example, we delete the new site collection that we previously created. We also add the GradualDelete switch parameter and set the Confirm switch parameter to $False so that no confirmation is required.