Introduction
This article provides guidance on how to use Microsoft Windows PowerShell commands to create a Microsoft SharePoint 2010/ SharePoint 2013 basic operations and it will be useful for SharePoint developers and admins.
This article will include the basic Create, Read, Update and Delete operations, using SharePoint 2013 sites.
PowerShell
PowerShell is a tool available in Microsoft to run/execute the cmdlets/.exe files to perform some operations.
MSDN says,”
Windows PowerShell™ command-line interface is a new command-line tool and supporting scripting language from Microsoft that complements Cmd.exe in the Windows administration context. In the SharePoint administration context, Windows PowerShell supersedes the Stsadm.exe administration tool. Moving forward, you should use Windows PowerShell scripting technology to develop any new command-line scripts in SharePoint Foundation 2010.
Ref
https://msdn.microsoft.com/en-us/library/office/ee539977(v=office.14).aspx
Before going to the article, we first need to know what is PowerShell, how the commands can use it and basic syntax; we should know it.
Learn about basic Windows PowerShell cmdlets, concepts and how to use Windows PowerShell with SharePoint 2013.
Ref
https://technet.microsoft.com/en-us/library/ee806878.aspx#sectionExPol
PowerShell/SharePoint Management Shell:
SharePoint Management Shell is available in your machine after you installed SharePoint2013.
Open SharePoint Management Shell
- Start - SharePoint Management Shell.
- SharePoint Management Shell is not on the Start screen:
- Computer - All appsàSharePoint Management Shell.
A site collection is a grouping/collection of the Websites under a common top-level
After you create a site collection, a top-level site is automatically created in the site collection. You can then create one or more sub sites under the top-level site in SharePoint2013.
Before we use this SharePoint Management Shell, the admin should be a user of Shell access db_owner.
For this, we have to use this command to add the user to the admin role.
Syntax
Add-SPShellAdmin [-UserName] <String> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-database <SPDatabasePipeBind>] [-WhatIf [<SwitchParameter>]]
When you run this cmdlet to add a user to the SharePoint_Shell_Access role,
- User must have membership in the securityadmin fixed Server role on the SQL Server instance.
- User must have membership in the db_owner fixed database role on all affected databases.
- User must have local administrative permission on the local computer.
Example
Add-SPShellAdmin -UserName spdev\gowtham
Create a site collection in SharePoint 2013
- Open your SharePoint Management Shell in your system.
- Copy the code given below.
- Run it.
Get-SPWebTemplate$template = Get-SPWebTemplate "STS#0"
New-SPSite -Url https://gowtham.sharepoint.com -OwnerAlias spdev\gowtham" -Template $template. Here, we used Team site Template "STS#0"
List of SharePoint 2013 Template sites
Here, the codes are given below, which are to be used in SharePoint site creation and use this template ID, which is based on your requirement.
Template ID | Title |
GLOBAL#0 | Global template |
STS#0 | Team Site |
STS#1 | Blank Site |
STS#2 | Document Workspace |
MPS#0 | Basic Meeting Workspace |
MPS#1 | Blank Meeting Workspace |
MPS#2 | Decision Meeting Workspace |
MPS#3 | Social Meeting Workspace |
MPS#4 | Multipage Meeting Workspace |
CENTRALADMIN#0 | Central Admin Site |
WIKI#0 | Wiki Site |
BLOG#0 | Blog |
SGS#0 | Group Work Site |
TENANTADMIN#0 | Tenant Admin Site |
APP#0 | App Template |
APPCATALOG#0 | App Catalog Site |
ACCSRV#0 | Access Services Site |
ACCSRV#1 | Assets Web Database |
ACCSRV#3 | Charitable Contributions Web Database |
ACCSRV#4 | Contacts Web Database |
ACCSRV#5 | Projects Web Database |
ACCSRV#6 | Issues Web Database |
ACCSVC#0 | Access Services Site Internal |
ACCSVC#1 | Access Services Site |
BDR#0 | Document Center |
DEV#0 | Developer Site |
DOCMARKETPLACESITE#0 | Academic Library |
EDISC#0 | eDiscovery Center |
EDISC#1 | eDiscovery Case |
OFFILE#0 | (obsolete) Records Center |
OFFILE#1 | Records Center |
OSRV#0 | Shared Services Administration Site |
PPSMASite#0 | PerformancePoint |
BICenterSite#0 | Business Intelligence Center |
SPS#0 | SharePoint Portal Server Site |
SPSPERS#0 | SharePoint Portal Server Personal Space |
SPSPERS#2 | Storage And Social SharePoint Portal Server Personal Space |
SPSPERS#3 | Storage Only SharePoint Portal Server Personal Space |
SPSPERS#4 | Social Only SharePoint Portal Server Personal Space |
SPSPERS#5 | Empty SharePoint Portal Server Personal Space |
SPSMSITE#0 | Personalization Site |
SPSTOC#0 | Contents area Template |
SPSTOPIC#0 | Topic area template |
SPSNEWS#0 | News Site |
CMSPUBLISHING#0 | Publishing Site |
BLANKINTERNET#0 | Publishing Site |
BLANKINTERNET#1 | Press Releases Site |
BLANKINTERNET#2 | Publishing Site with Workflow |
SPSNHOME#0 | News Site |
SPSSITES#0 | Site Directory |
SPSCOMMU#0 | Community area template |
SPSREPORTCENTER#0 | Report Center |
SPSPORTAL#0 | Collaboration Portal |
SRCHCEN#0 | Enterprise Search Center |
PROFILES#0 | Profiles |
BLANKINTERNETCONTAINER#0 | Publishing Portal |
SPSMSITEHOST#0 | My Site Host |
ENTERWIKI#0 | Enterprise Wiki |
PROJECTSITE#0 | Project Site |
PRODUCTCATALOG#0 | Product Catalog |
COMMUNITY#0 | Community Site |
COMMUNITYPORTAL#0 | Community Portal |
SRCHCENTERLITE#0 | Basic Search Center |
SRCHCENTERLITE#1 | Basic Search Center |
SRCHCENTERFAST#0 | FAST Search Center |
visprus#0 | Visio Process Repository |
Delete a site collection in SharePoint 2013
If you are deleting a SharePoint site collection, you may lose your all subsites under the site collection . When you delete a site collection, you permanently destroy all the content and the user information, as shown below.
- Documents and document libraries.
- Site configuration settings and user profile Service configurations.
- Groups, Role and security information, which is related to the website.
- Sub sites of the top-level website, their contents and user information.
To avoid any data loss, first try to take backup your site collection, then try these powerful commands.
Remove-SPSite -Identity https://gowtham.sharepoint.com GradualDelete
Restore a site collection in SharePoint 2013
This cmdlets describes how to restore a deleted site collection in SharePoint 2013.
Syntax
Restore-SPDeletedSite [-Identity] <SPDeletedSitePipeBind> [-AssignmentCollection <SPAssignmentCollection>] [-Confirm [<SwitchParameter>]] [-ContentDatabase <SPContentDatabasePipeBind>] [-WebApplication <SPWebApplicationPipeBind>] [-WhatIf [<SwitchParameter>]]
Example
Restore-SPDeletedSite -Identity 510857cb-7414-4a89-8bf3-ad3628f6c8c6
Create a sub site in SharePoint 2013
To add a new sub site, use the New-SPWeb cmdlet
$WebAppUrl="http://gowtham.sharepoint.com/"
$SubSiteTitle1 = "TestTitle"
$SubSite1Url = "TestSubsite"
New-SPWeb –url $WebAppUrl$SubSite1Url -name $SubSiteTitle1 -Template "STS#0"
–AddToTopNav –UniquePermissions –UseParentTopNav
Delete a sub site in SharePoint 2013
The Remove-SPWeb cmdlet completely deletes the Web specified by the Identity parameter.
Example
Remove-SPWeb http://gowtham.sharepoint.com/ TestSubsite
Delete all sub sites in SharePoint 2013
This code will get all the sub sites from the site collection and delete them one by one from the bottom up.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$StartWeb=https://gowtham.sharepoint.com/test
$subsites = ((Get-SPWeb $StartWeb).Site).allwebs | ?{$_.url -like "$StartWeb*"} foreach($subsite in $subsites) { write-host "Deleting Site " $subsite.url Remove-SPWeb $subsite.url -Confirm:$false }}
Conclusion
This article helped you to understand about SharePoint basic operations with PowerShell commands. In this article, SharePoint Sites /Web basic CRUD operations were explained. In the next article, we will see about same basic operations in SharePoint lists and libraries, using PowerShell method.