Basic Operations In SharePoint 2013 Using PowerShell

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 IDTitle
GLOBAL#0Global template
STS#0Team Site
STS#1Blank Site
STS#2Document Workspace
MPS#0Basic Meeting Workspace
MPS#1Blank Meeting Workspace
MPS#2Decision Meeting Workspace
MPS#3Social Meeting Workspace
MPS#4Multipage Meeting Workspace
CENTRALADMIN#0Central Admin Site
WIKI#0Wiki Site
BLOG#0Blog
SGS#0Group Work Site
TENANTADMIN#0Tenant Admin Site
APP#0App Template
APPCATALOG#0App Catalog Site
ACCSRV#0Access Services Site
ACCSRV#1Assets Web Database
ACCSRV#3Charitable Contributions Web Database
ACCSRV#4Contacts Web Database
ACCSRV#5Projects Web Database
ACCSRV#6Issues Web Database
ACCSVC#0Access Services Site Internal
ACCSVC#1Access Services Site
BDR#0Document Center
DEV#0Developer Site
DOCMARKETPLACESITE#0Academic Library
EDISC#0eDiscovery Center
EDISC#1eDiscovery Case
OFFILE#0(obsolete) Records Center
OFFILE#1Records Center
OSRV#0Shared Services Administration Site
PPSMASite#0PerformancePoint
BICenterSite#0Business Intelligence Center
SPS#0SharePoint Portal Server Site
SPSPERS#0SharePoint Portal Server Personal Space
SPSPERS#2Storage And Social SharePoint Portal Server Personal Space
SPSPERS#3Storage Only SharePoint Portal Server Personal Space
SPSPERS#4Social Only SharePoint Portal Server Personal Space
SPSPERS#5Empty SharePoint Portal Server Personal Space
SPSMSITE#0Personalization Site
SPSTOC#0Contents area Template
SPSTOPIC#0Topic area template
SPSNEWS#0News Site
CMSPUBLISHING#0Publishing Site
BLANKINTERNET#0Publishing Site
BLANKINTERNET#1Press Releases Site
BLANKINTERNET#2Publishing Site with Workflow
SPSNHOME#0News Site
SPSSITES#0Site Directory
SPSCOMMU#0Community area template
SPSREPORTCENTER#0Report Center
SPSPORTAL#0Collaboration Portal
SRCHCEN#0Enterprise Search Center
PROFILES#0Profiles
BLANKINTERNETCONTAINER#0Publishing Portal
SPSMSITEHOST#0My Site Host
ENTERWIKI#0Enterprise Wiki
PROJECTSITE#0Project Site
PRODUCTCATALOG#0Product Catalog
COMMUNITY#0Community Site
COMMUNITYPORTAL#0Community Portal
SRCHCENTERLITE#0Basic Search Center
SRCHCENTERLITE#1 Basic Search Center
SRCHCENTERFAST#0FAST Search Center
visprus#0Visio 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.