SharePoint 2013/2016 PowerShell Script Examples

This SharePoint PowerShell tutorial explains, various SharePoint 2013 PowerShell script examples to create web applications, site collection, subsite, list or document library in SharePoint 2013/2016. You will see how to create a web application, site collection, subsite or list using PowerShell in SharePoint 2013.

If you are new to PowerShell, then you can read a detailed tutorial on Working with PowerShell in SharePoint Online/2016/2013.

You can use Windows PowerShell ISE or Visual Studio code to write, run and debug PowerShell script.

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
  • Hide Yes/No column using PowerShell in SharePoint Site Content Type
  • Get Users who is not having My site from the User Profiles in SharePoint using PowerShell

Create Web Application using PowerShell in SharePoint 2013/2016

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 SharePoint 2016, you will get below warning because from SharePoint 2013 onwards Microsoft has an 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 the 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 the authentication provider and various type of authentication providers.

Authentication Provider:
The authentication method is a specific exchange of account credentials and other information that asserts 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 the FARM Admin account has DB creation access in the SQL server if not you can’t create web application.

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

Create Site Collection using PowerShell in SharePoint 2013/2016

As per the Microsoft standards, we can create 750,000 per farm. In SharePoint we have various default templates to create Site Collection, these templates create 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 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 using PowerShell in SharePoint 2013.

Create Sub Site in Site Collection using PowerShell in SharePoint 2013/2016

As per the Microsoft standards, we can create 250,000 subsites per site collection and the syntax follows like this:

Syntax:

New-SPWeb -Template "STS#0"

Example:

Create a subsite using Powershell

New-SPWeb http://:55245/COSTCOInventory -Template "STS#0"

Update 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()

Create SharePoint List using PowerShell in SharePoint 2013/2016

In SharePoint, we maintain data in lists & libraries and below is the Syntax for creating SharePoint List. To create a 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:

Create SharePoint List using Powershell

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

Create Field / Column in a List using PowerShell in SharePoint 2013/2016

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

Syntax:

$SPList.Fields.Add("","",)

Create 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.

Hide Yes/No column using PowerShell in SharePoint Site Content Type

Here, we will see how to hide Yes/No column type from a Site content type in SharePoint 2013 using PowerShell.

I got a requirement, to hide a Yes/No Field in a Custom list called “ABC”. But after analysis came to know that field is coming from the content type called “XYZ”. So my first approach is from Visual Studio and made a code change to that field and added a property called “hidden= true”.

But that field property is not reflecting in the SharePoint list not even in Content type as well, from other blogs and colleagues, suggestions came to know that you cannot hide an existing field of type “yes/no” from .wsp deployment. So, I found a way that using PScript we can hide the yes/no field.

Below is the PowerShell code, which you can write inside a Windows PowerShell ISE.

cls
Add-PsSnapin Microsoft.SharePoint.PowerShell
$site = Get-SPSite "Site URL"
$web = $site.RootWeb
$web.AllowUnsafeUpdates = $true
$ct = $web.ContentTypes['Content Type Name'] $field = $ct.FieldLinks[" FieldInternalName "] $field.Hidden = $true
$ct.Update($true)
$web.AllowUnsafeUpdates = $false
Write-Host "Field is hidden in the content type"
$web.Dispose()

Once PSScript executed.you can verify in the content type you can see that field has been hidden.

Here, we learned how to hide Yes/No column using PowerShell in SharePoint Site Content Type.

Get Users who is not having My site from the User Profiles in SharePoint using PowerShell

By using Powershell Script we can find whether the user is having MySite Profile in SharePoint. The Script follows below.

cls
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$site = new-object Microsoft.SharePoint.SPSite("Site URL");
$Context = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);
$upAttribute = "SPS-PersonalSiteCapabilities"
$PeopleManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($Context)
$profiles = $PeopleManager.GetEnumerator()
Foreach ($User in $profiles)
{
If ($User[$upAttribute].tostring() -ne $null)
{
Write-Host "User who is not having my site:" $User.AccountName -ForegroundColor Green
}
}
write-host "Finished."
$site.Dispose()

You may like following SharePoint PowerShell tutorials:

Here we learned how to get users not having My site from the user profiles in SharePoint using PowerShell.

I hope this PowerShell tutorial helps to learn how to create a web application, site collection, subsite, and list using PowerShell in SharePoint 2013.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

>