Using PowerShell in SharePoint 2013

Posted by: Pravinkumar Dabade , on 3/1/2014, in Category Powershell
Views: 37800
Abstract: In this article, we take an overview of PowerShell and demonstrate some examples of using PowerShell in SharePoint 2013.

Microsoft introduced PowerShell in 2003 as an advanced automation-script language which replaces legacy DOS batch files. Since PowerShell is built on top of the .NET Framework and makes use of programming concepts from C#/VB.NET, it becomes easier for a .NET professional to write scripts and manipulate .NET objects.

For our new enthusiasts who have just joined the party, let's get started with the question "Why Should I use PowerShell for performing operations in SharePoint?".

Administrators love Scripts and PowerShell allows you to script SharePoint via API’s. That’s a good enough reason to use PowerShell, right? With SharePoint 2010, Microsoft released several commandlets (cmdlets) to automate and administer SharePoint. Now with SharePoint 2013, PowerShell has become the de-facto standard for configuring SharePoint. In fact, there are certain operations in SharePoint that can be performed only in PowerShell, for example - Search Topology configuration on production servers.

This article is not a tutorial on PowerShell scripting. It rather gives an overview of how PowerShell commands work and then proceeds to show how these commands can be used to perform repeating administrative tasks in the SharePoint Environment.

This article is published from the DotNetCurry .NET Magazine – A Free High Quality Digital Magazine for .NET professionals published once every two months. Subscribe to this eMagazine for Free and get access to hundreds of free tutorials from experts

For this article, let's consider a distinct set of audience, each having their own uses of PowerShell. For example, you have Administrators, IT Pros or Developers who would like to use PowerShell for different reasons.

IT Pros may want to automate couple of tasks like maintenance tasks or site collection provisioning tasks. They can write PowerShell scripts and make it reusable in your organization's farm. Similarly your Administration department may request some features to be made available to Developers. This allows them to make use of PowerShell script to perform jobs.

Let's get started with some examples of using PowerShell to perform some tasks. There are around 500 commands (cmdlets) that can be used in SharePoint 2010 and about 800 commands in SharePoint 2013. Apart from the commands provided by Microsoft, there are several community command-lets which we can use while working with PowerShell. When you work with PowerShell, you must have appropriate permissions to execute the PowerShell commands.

PowerShell is included in Windows 7 & Windows Server 2008 R2 or higher machines and appears in the Windows Taskbar on Server machines. You can launch a plain vanilla PowerShell window by clicking on the PowerShell icon in the taskbar. To make this instance aware of SharePoint, you can add the SharePoint PowerShell add-in using

Add-PSSnapin Microsoft.SharePoint.PowerShell

In SharePoint 2013, a SharePoint-branded PowerShell is available with all cmdlets preinstalled called SharePoint 2013 Management Shell. You can access this shell in Windows 2008 R2 from Start > Microsoft SharePoint 2013 Products > SharePoint 2013 Management Shell. In Windows 2012, you can type SharePoint 2013 Management Shell to start the shell from Start Menu. This management shell is aware about SharePoint commands as well as general PowerShell Commands.

To see all the commands, just use the command -

Get-Command -PSSnapin Microsoft.SharePoint.PowerShell

A couple of things to remember -

  • While working with command-let, the syntax is not case sensitive.
  • You can also make use of TAB to complete the commands in PowerShell.
  • In case you are performing some critical operations using PowerShell, PowerShell has in-built protection for such commands which will ask you for a confirmation. But in some cases, while you are doing batch processing, you can override this behaviour by using [-Confirm:$false].
  • You can use | [pipeline] character to pass the object to the next cmdlet.
  • You can use aliases for a couple of commands like cls is for Clear-Host, Where or ? for Where-Object, gm for Get-Member etc.

Now let's open PowerShell and try some commands as shown below -


When you work with PowerShell, most of the operations you perform are on .NET objects. In .NET, an object is described with the help of its characteristics [Properties] and the behaviours [Methods]. You can use the same when working with PowerShell. For example, you can use a remove() method to remove a SPSite.

Declare Variables, Filters and Operators

Let’s see how to declare variables, filters and use different operators while working with PowerShell. The declaration is shown below –

Variable declaration –

- A variable declaration always starts with $.

- You can assign values to variables like § $projectName = “BI Portal”

Filters –

- You can use filters while passing the object to the next cmdlet in a pipeline.

- Filters are declared using Where-Object [can be aliased “Where”, “?”]

- For example - Get-SPSite "http://itechy:21237/sites/itechyprojects" | Get-SPWeb -Limit All | Where-Object {$_.WebTemplate -eq "PROJECTSITE"} |select Url, WebTemplate, Title

Operators –

You can use number of operators while working with PowerShell –

Comparison Operators –

  • -eq – Equal to operator.
  • -lt – Less than operator.
  • -gt – Greater than operator.
  • -le – Less than or equal to.
  • -ge – Greater than or equal to.
  • -like – pattern matching

Logical Operator –

  • -and
  • -or
  • !, -not

Let’s try writing some commands which will use variables, pipelines, comparison operators, the objects, properties and methods as described below –


Conditions and Loops in PowerShell

Now we will take a look at how do we write conditions, loops in PowerShell which can be used during scripting.

If / Elseif / Else Condition

Here’s how to perform tasks based on the If condition.

$conditionVariable = Get-SPSite “http://itechy/sites/
biportal” | Get-SPWeb | Select WebTemplate
if($conditionVariable.WebTemplate -eq “BICENTERSITE”)
Write-Host “We are working on Microsoft SharePoint BI
Write-Host “The Default is Team Site Template”

While Condition

Here’s how to use the while condition while writing the scripts.

if($services.TypeName -eq “PerformancePoint Service”
-and $services.Status -eq “ONLINE”) {
Write-Host “Performance Point Service found!!”
else {
Write-Host “Not found”


You can also wrap a reusable script into a function and call it as per your requirements. You can also pass parameters to your functions.

function CheckServiceAndStatus([string]$name)
if($services.TypeName -eq $name)
Write-Host “$($name) found!!”
Write-Host “Not found”

To use this command, type this

CheckServiceAndStatus -name "PerformancePoint Service"

I have written this function in a script file.

Using PowerShell in SharePoint 2013

Let’s see some examples of PowerShell in SharePoint 2013. With SharePoint Server 2013, we get PowerShell version 3.0. To see the current PowerShell version on your machine, we can use the following command –


Note: PowerShell version 3.0 requires .NET Framework 4.0 and as you know SharePoint Server 2010, does not support .NET 4.0. Hence you can use a side-by-side approach for executing SharePoint 2010 cmdlets on PowerShell.

You can also use graphical help under PowerShell V 3.0. For example, take a look at the following command –


Similarly you can also apply filters with graphical help by clicking on “Settings” button shown on the right side of the window. You also have a search functionality in the graphical window. The screenshot we just saw shows the search result for SPWebApplication.

When you execute a PowerShell command, you can now send the output of the command to a separate, interactive table window where you can add filters and different criteria to the output which is getting displayed.

Let’s try the following command that uses a filter and sends output to a separate window –

$singleSite = Get-SPWeb "http://itechy/sites/BIPortal”

$singleSite.Lists | Select Title

$singleSite.Lists | where {$_.ItemCount -lt 50} | select Title, ItemCount | Out-GridView

Compared to SharePoint 2010, there are several cmdlets  enhancements done in SharePoint 2013.

For example –

PowerShell V2 –

$myObject = New-Object -TypeName PSCustomObject -Property @{Name="MSBI"}

$myObject | select *

PowerShell V3 –

$myNewObject = [PSCustomObject] @{Name="MSBI"}

$myNewObject | select *

You can also write cmdlets for Office 365. To write scripts for Office 365, you will have to download SharePoint Online Management Shell from the following URL –

Let’s quickly see some SharePoint Online cmdlets. I have already registered on the Office 365 developer site. First of all, we will connect to the SharePoint site as shown below –

To establish connection to SharePoint, we use the following command –

Connect-SPOService -Url https://itechy-admin.sharepoint. com -Credential “

The following command retrieves the existing site collection –


The –detailed parameter gets all the site collections under your subscription. When you try the Get-SPOSite – Detailed parameter, you will see all the property values. However if you do not supply the –detailed parameter, all the property values will come with default values other than few core property values.

Get-SPOSite https://itechy-admin.sharepoint. com –detailed | select *

Get-SPOSite | select *

To view all the available templates, we can use the following command


This way you can use PowerShell to write scripts to perform various operations with our SharePoint Site as an IT Pro or as a Developer.


With SharePoint 2013 embracing PowerShell as a first class citizen, PowerShell has become a standard for scripting SharePoint. This article attempted to introduce you to PowerShell, however being a very vast topic, we will discuss some additional commands in one of our forthcoming articles.

Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+
Further Reading - Articles You May Like!
Pravinkumar, works as a freelance trainer and consultant on Microsoft Technologies. He is having over 10 years of experience in IT and is also a Microsoft Certified Trainer(MCT). He has conducted various corporate trainings on all versions of .NET Technologies including .NET, SharePoint Server, Microsoft SQL Server, Silverlight, ASP.NET, Microsoft PerformancePoint Server 2007 (Monitoring). He is passionate about learning new technologies from Microsoft. You can contact Pravinkumar at dabade[dot]pravinkumar [attherate] gmail[dot]com

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Milller on Thursday, March 6, 2014 7:42 AM
Powershell comes handy in our SharePoint admin tasks. Thanks for sharing some tips
Comment posted by Jeet on Tuesday, March 18, 2014 9:23 AM
Hi Praveen,

Can you please provide me - your mail id .
I need help regarding issues related with- SP powershell cmd, SP data collection issue, SP client issues etc.

Please Help me.......

Thanks in advance,
Comment posted by Vaishnavi on Tuesday, April 22, 2014 12:08 AM
Hi Pravin,

   Your post is very useful for beginners like me.

Can you explain me how to run two powershell scripts combined together as one file and run it using a batch file in sharepoint 2010.

Thanks in Advance.






Free DNC .NET Magazine



jQuery CookBook