SharePoint 2013 PowerShell Script Examples

sharepoint 2013 powershell script examples

In this SharePoint 2013 tutorial we will discuss about various PowerShell script examples in SharePoint 2013. The same PowerShell commands we can also use in SharePoint 2016.

Introduction:

Here we will learn how to create below SharePoint artifacts using PowerShell:

  • Create Web Application using PowerShell SharePoint 2013
  • Create Site Collection using PowerShell SharePoint 2013
  • Create Sub Site using PowerShell SharePoint 2013
  • Create List using PowerShell SharePoint 2013
  • Create List item using PowerShell SharePoint 2013

Create Web Application using PowerShell in SharePoint 2013:

As per the Microsoft standards we can create 10 Web Applications for the best performance and the syntax as follows.

Syntax:
New-SPWebApplication -ApplicationPool -Name [-AdditionalClaimProvider <SPClaimProviderPipeBind[]>] [-AllowAnonymousAccess ] [-ApplicationPoolAccount ] [-AssignmentCollection ] [-AuthenticationMethod ] [-AuthenticationProvider <SPAuthenticationProviderPipeBind[]>] [-Confirm []] [-DatabaseCredentials ] [-DatabaseName ] [-DatabaseServer ] [-HostHeader ] [-Path ] [-Port ] [-SecureSocketsLayer ] [-ServiceApplicationProxyGroup ] [-SignInRedirectProvider ] [-SignInRedirectURL ] [-Url ] [-UserSettingsProvider ] [-WhatIf []]

Example:
New-SPWebApplication -Name “COSTCO Inventory Application” -Port 55245 -HostHeader COSTCOInventory.com -URL “http://www.COSTCOInventory.com” -ApplicationPool “COSTCOInventory” -ApplicationPoolAccount (Get-SPManagedAccount “Domain\FARMADMINAccount”) -DatabaseName “COSTCOInventory_DB01”



In SharePoint 2013 and 2016 you will get below warning because from SharePoint 2013 onwards Microsoft has obsoleted Classic authentication method which was the default till SharePoint 2010.

WARNING: The Windows Classic authentication method is deprecated in this release and the default behavior of this cmdlet, which creates Windows Classic based web application, is obsolete. It is recommended to use Claims authentication methods. You can create a web application that uses Claims authentication method by specifying the AuthenticationProvider parameter set in this cmdlet. Refer to the http://go.microsoft.com/fwlink/?LinkId=234549 site for more information. Please note that the default behavior of this cmdlet is expected to change in the future release to create a Claims authentication based web application instead of a Windows Classic based web application.

Now let us learn what is authentication provider and various type of authentication providers.

Authentication Provider:
Authentication method is a specific exchange of account credentials and other information that assert a user’s identity. The result of the authentication method is proof, typically in the form of a token that contains claims, that an authentication provider has authenticated a user.

Type of Authentication Providers in SharePoint 2013 & 2016:

  • Claims-based
  • Windows based
  • Forms based
  • Security Assertion Markup Language (SAML) token-based

For more information on Authentication providers https://technet.microsoft.com/en-us/library/cc262350.aspx

Now let us create the web application with default authentication provider which is “Claims-based”

Example:
A) With HOSTHeader parameter:
$ap = New-SPAuthenticationProvider
New-SPWebApplication -Name “COSTCO Inventory Application” -Port 55245 -HostHeader COSTCOInventory.com -URL “http://www.COSTCOInventory.com” -ApplicationPool “COSTCOInventory” -ApplicationPoolAccount (Get-SPManagedAccount ” Domain\FARMADMINAccount”) -DatabaseName “COSTCOInventory_DB01” -AuthenticationProvider $ap

B) Without HostHeader parameter:
$ap = New-SPAuthenticationProvider
New-SPWebApplication -Name “COSTCO Inventory Application” -Port 55245 -URL “http://www.” -ApplicationPool “COSTCOInventory” -ApplicationPoolAccount (Get-SPManagedAccount “DOMAIN\ FARMADMINAccount”) -DatabaseName “COSTCOInventory_DB01” -AuthenticationProvider $ap

Note: Ensure that FARM Admin account has DB creation access in SQL server if not you canot create the web application.

Now that we have created web application successfully we need to create at least one site collection called as “Top Level Site Collection”. Without this site collection we can’t even access the web application we created.

Creating Site Collection using PowerShell in SharePoint 2013:

As per the Microsoft standards we can create 750,000 per farm. In SharePoint we have various default templates to create SiteCollection, these templates creates the site collection and also create the default artifacts like some sample list and libraries.



To know the list and its template code, execute the below command in powershell:
Get-SPWebTemplate

The output will look like as below:

sharepoint 2013 powershell script examples
sharepoint 2013 powershell script examples

Syntax for Site Collection Creation:
Get-SPWebTemplate

$template = Get-SPWebTemplate “STS#0”

New-SPSite -Url “” -OwnerAlias “<domain\user>” -Template $template

Example:
$template = Get-SPWebTemplate “STS#0”
New-SPSite -Url “http://:55245/” -OwnerAlias “DOMAIN\FARM ADMINISTRATOR” -Template $template

With this step we have created Site Collection successfully.

Creating Sub Site in Site Collection using PowerShell in SharePoint 2013:

As per the Microsoft standards, we can create 250,000 sub sites per site collection and the syntax follows as this:

Syntax:
New-SPWeb -Template “STS#0”

Example:
— Creating a subsite using Powershell
New-SPWeb http://:55245/COSTCOInventory -Template “STS#0”

— Updating Subsite Title
$web = get-spweb(“http://:55245/COSTCOInventory”);
$web.TitleResource.SetValueForUICulture($web.UICulture, “COSTCO Inventory”);
$web.update()

(or)

$web = get-spweb(“http://:55245/COSTCOInventory”);
$web.Title = “COSTCO Inventory”;
$web.update()

Creating SharePoint List using PowerShell in SharePoint 2013:

In SharePoint we maintain data in lists & libraries and the below is the Syntax for creating SharePoint List. To create list in a SharePoint site we have to first get the Site object and the type of the list template. The syntax is as follows:

Syntax:
$SPweb = Get-SPWeb -Identity ”
$ListTemplate = $SPweb.ListTemplates[”]
$SPweb.Lists.Add(”, ” , $ListTemplate)

Example:
— Creating List using Powershell
$SPweb = Get-SPWeb -Identity ‘http://:55245/COSTCOInventory’
$ListTemplate = $SPweb.ListTemplates[‘Custom List’]

Creating Field / Column in a List using PowerShell in SharePoint 2013:

We can create the columns in list with the below syntax:

Syntax:
$SPList.Fields.Add(“”,””,)

Example:

— Creating Column / Field in Sharepoint List
$SPweb = Get-SPWeb -Identity ‘http://:55245/COSTCOInventory’
$SPList = $SPweb.Lists[‘NewStockDetails’]
$SPList.Fields.Add(“First Name”,”Text”,0) — In this example we setting the required field false.

Hope this article will be helpful.

Similar SharePoint 2013 Tutorials


Leave a Reply