How to use PowerShell in SharePoint Online/2016/2013

In this PowerShell SharePoint tutorial, I will explain how you can work with PowerShell in SharePoint Online as well as SharePoint on-premises versions like SharePoint 2013/2016. We will see how to connect to SharePoint Online in PowerShell, as well as various PowerShell SharePoint commands for SharePoint list and document libraries.

By reading this SharePoint PowerShell tutorial, you will learn various PowerShell SharePoint command and PowerShell SharePoint snapin and will get to know various PowerShell commands for SharePoint 2013/2016 administrators.

Introduction to PowerShell SharePoint

PowerShell is a command line scripting language and we can use PowerShell in SharePoint which will provide administrators to give full access to SharePoint objects.

By using PowerShell in SharePoint, Administrators can work directly with SharePoint objects like web application, site collection, sites/subsites, list, libraries, field/columns, etc.

By default, in any Windows operating system, you will find PowerShell in the below location:
<%SystemRoot%>\System32\WindowsPowerShell\v1.0\PowerShell.exe.

If you are new to PowerShell you can read Working with PowerShell Variables, How to create and use PowerShell global variable, and also you can download top 51 PowerShell examples Free.

To write, debug and test PowerShell scripts/commands or snapin, you can either use Windows PowerShell ISE or you can use Visual Studio Code which is FREE to use.

Now we will see how we can use PowerShell in SharePoint 2013/2016 and SharePoint Online.

PowerShell SharePoint 2013/2016

Once you install SharePoint 2016/2013 in a SharePoint server, PowerShell cmdlets are available in the SharePoint 2016 management shell or SharePoint 2013 management shell. You do not need to install any other additional things to work in PowerShell SharePoint On-premise.

In a SharePoint server, all PowerShell SharePoint cmdlets are available in Microsoft.SharePoint.PowerShell.dll.

If you are not using SharePoint 2013/2016 management shell, using Windows PowerShell ISE then you have to manually register Microsoft.SharePoint.PowerShell.dll module for SharePoint Server cmdlets. You can do this by adding the below line at the beginning of a PowerShell file.

Add-PSSnapin Microsoft.SharePoint.PowerShell

or you can also add the below lines.

$snap = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.PowerShell'}
 if ($snap -eq $null) {
 Add-PSSnapin Microsoft.SharePoint.PowerShell
 }

Permissions requires for PowerShell SharePoint

To work with PowerShell SharePoint 2013/2016, the account you are using should have securityadmin server role membership in SQL Server. And the account also should be a member of Administrators group.

Remember, the account you have used while installing SharePoint 2016/2013, provided with proper permissions to run or execute PowerShell cmdlets in SharePoint server.

Note: Always use Open SharePoint management shell or PowerShell ISE as Run as administrator.

PowerShell SharePoint cmdlets for Solution deployment

You can use various PowerShell SharePoint cmdlets for deploying SharePoint farm solutions to SharePoint server 2013/2016.
We can use below PowerShell SharePoint cmdlets for SharePoint farm solution deployment.

  • Add-SPSolution
  • Install-SPSolution
  • Update-SPSolution
  • Uninstall-SPSolution

You can check below two PowerShell SharePoint tutorials for more information.

You can use below PowerShell SharePoint cmdlets for SharePoint sandboxed solution deployment in SharePoint.

  • Add-SPUserSolution
  • Install Sandboxed Solution
  • Uninstall Sandboxed Solution
  • Remove Sandboxed Solution
  • Update Sandboxed Solution

PowerShell SharePoint Site Operation

We can use various PowerShell SharePoint cmdlets to work with SharePoint 2013/2016 sites. You can use PowerShell cmdlets to create a SharePoint site, retrieve SharePoint site, subsite information, etc.

PowerShell SharePoint cmdlets to change SharePoint site logo

We can use PowerShell SharePoint cmdlets to change SharePoint site collection site or subsite logo. You can check an article on How to change logo in the SharePoint site using PowerShell.

PowerShell SharePoint List or Document Library Opertations

We can use various PowerShell SharePoint cmdlets to work with the SharePoint 2013/2016 list or document library. We can use PowerShell cmdlets to create, update and delete list. We can insert, update and delete list items using PowerShell in SharePoint. Similarly, we can also use PowerShell SharePoint cmdlets to upload documents to SharePoint 2013/2016 document library.

Below are various PowerShell SharePoint examples to work with SharePoint 2013/2016 list or document library.

  • Create document library sharepoint online powershell: This PowerShell SharePoint tutorial explains how to create SharePoint 2013/2016/Online document library using PowerShell.
  • Upload bulk documents to SharePoint 2016 document library using PowerShell: This SharePoint PowerShell tutorial explains how to upload bulk documents to SharePoint 2016/2013 document library using PowerShell.
  • How to create and remove indexed column using PowerShell in SharePoint Online/2013/2016 list or document library?: This SharePoint PowerShell tutorial explains how to create and remove an indexed column in SharePoint 2013/2016 list or document library.
  • SharePoint Online Create Workflow History List using PowerShell or using SharePoint Designer 2013: This PowerShell SharePoint tutorial explains how we can create a workflow history list using PowerShell. We can not create a Workflow History list using a browser in SharePoint 2013/2016.
  • Update User Email-ID using PowerShell in SharePoint 2013: This SharePoint PowerShell tutorial explains how you can update user’s email id using PowerShell in SharePoint 2013/2016.
  • Upload document using PowerShell Remotely: By following this SharePoint PowerShell tutorial, you can able to upload documents to SharePoint 2013/2016 document library.
  • Upload document using PowerShell Remotely with default account: This SharePoint PowerShell tutorial explains how to upload documents to document library remotely with default account in SharePoint 2013.
  • Download all files from document libraries using PowerShell in SharePoint 2013 or SharePoint 2016: This PowerShell SharePoint explains, how to download all files or documents from SharePoint document library in SharePoint 2013/2016.
  • Steps to add items from csv file to SharePoint Online List using PowerShell in CSOM: This PowerShell SharePoint explains how to add items from .CSV file to SharePoint list.
  • SharePoint 2013 Create Site Column and Content Types Using PowerShell: This PowerShell SharePoint example, explains how to create site column and content types using PowerShell in SharePoint 2013/2016/
  • Migrate files with folder structure to SharePoint 2013 document library using PowerShell:
  • Add item to SharePoint list using PowerShell: This PowerShell SharePoint tutorial explains how to add items to SharePoint 2013/2016 list using PowerShell.
  • How to cancel all workflows from one List in SharePoint using PowerShell?: By using this PowerShell SharePoint cmdlets you can cancel all workflows from one list in SharePoint site.
  • How to delete list using PowerShell in SharePoint 2013?: By using PowerShell SharePoint cmdlets, you can delete a list in SharePoint 2013/2016.
  • SharePoint 2013 Delete files or items from document library or list using PowerShell with conditions like created one month before:
  • Enable or Disable List Throttling using PowerShell in SharePoint 2013: You can easily enable or disable list throttling using PowerShell in SharePoint 2013/2016 site.

PowerShell SharePoint Commands for Backup and Restore SharePoint Site Collection or Subsites

We can use various PowerShell SharePoint commands to backup and restore SharePoint site collection, sub sites in SharePoint 2013/2016.

  • Backup and Restore content database SharePoint 2013: This PowerShell SharePoint tutorial explains how to take backup and restore content database in SharePoint 2013/2016.
  • Backup and Restore Site collection in SharePoint 2016/2013/2010/2007: This PowerShell SharePoint 2013 tutorial explains how to backup and restore SharePoint 2013/2016 site collection using PowerShell.
  • Backup and Restore in SharePoint 2010 using PowerShell: This SharePoint InfoPath tutorial explains how to create backup and restore in SharePoint 2010 using PowerShell.
  • SharePoint 2013 backup and restore using PowerShell: This PowerShell SharePoint 2013 tutorial explains, How to backup and restore in SharePoint 2013 using PowerShell and also how to backup and restore in SharePoint 2016 using SharePoint Central Administration.

Create a Web Application using PowerShell in SharePoint 2013/2016

The below PowerShell script helps on how to create a web application in SharePoint 2016/2013 using PowerShell.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"
$authentication = New-SPAuthenticationProvider -UseWindowsIntegratedAuthentication -DisableKerberos
New-SPWebApplication -Name "MyDemoWebApp" -Port 33780 -ApplicationPool "MySP2016AppPool" -ApplicationPoolAccount (Get-SPManagedAccount "MYSP\Administrator") -AuthenticationMethod NTLM -AuthenticationProvider $authentication

You can see the SharePoint web application created like below successfully.

SharePoint 2013 working with PowerShell
SharePoint 2013 working with PowerShell

Create a SharePoint Site Collection using PowerShell

The below PowerShell script helps on how to create a site collection in SharePoint 2013 using PowerShell.

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

New-SPSite http://sp2016:33779/sites/MySP2016SiteCollection -OwnerAlias "MYSP\Administrator" -Name "My SP2016 Site Collection" -Template "STS#0"

Retrieve SharePoint Online Current Site Title and URL using PowerShell

Now, let us see, how to retrieve the current site title and current site URL using PowerShell in SharePoint Online? How to retrieve parent web title and parent web URL using PowerShell in SharePoint Online? And how to retrieve root web title and root web URL using PowerShell in SharePoint Online.

I have a SharePoint online nested subsite like Bhawana ( Root web site ) -> MyTest (First level subsite) -> MyTestSubWeb (Second level subsite).

In the below PowerShell script, I am taking the context of the second level sub site and from there we will retrieve the parent web as well as root web.

Once you run the below PowerShell script, it will ask you for the credentials for the SharePoint online user with which you want to connect to the SharePoint online site.

Provide the password and it will display the above result. You can run, debug and test the PowerShell script using Visual studio code or by using Windows PowerShell ISE.

Get current site title and current site URL using PowerShell in SharePoint Online

Below PowerShell script to retrieve current site title and current site URL using PowerShell in SharePoint Online.

Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MyTest/MyTestSubWeb/"
$username = "bijay@onlysharepoint2013.onmicrosoft.com"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$rootWeb = $ctx.Web
$ctx.Load($rootWeb)
$ctx.ExecuteQuery()
Write-Host "Site Title: " $rootWeb.Title
Write-Host "Site URL: "$rootWeb.Url

Once you run the PowerShell script, you can see the output like below:

Retrieve current site title and current site url using PowerShell in SharePoint Online
Retrieve current site title and current site url using PowerShell in SharePoint Online

Get parent web title and parent web URL using PowerShell in SharePoint Online

Below is the PowerShell script to retrieve Parent web tile and parent web URL using PowerShell in SharePoint Online.

Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MyTest/MyTestSubWeb/"
$username = "bijay@onlysharepoint2013.onmicrosoft.com"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$rootWeb = $ctx.Web
$ctx.Load($rootWeb.ParentWeb)
$ctx.ExecuteQuery()
Write-Host "ParentWeb Title: " $rootWeb.ParentWeb.Title
Write-Host "ParentWeb Server Relative URL: " $rootWeb.ParentWeb.ServerRelativeUrl

You can see the output like below:

Retrieve parent web title and parent web url using PowerShell in SharePoint Online
Retrieve parent web title and parent web url using PowerShell in SharePoint Online

Get root web title and root web URL using PowerShell in SharePoint Online

Below is the PowerShell script to retrieve root web title and URL in SharePoint online.

Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MyTest/MyTestSubWeb/"
$username = "bijay@onlysharepoint2013.onmicrosoft.com"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$ctx.Load($ctx.Site.RootWeb)
$ctx.ExecuteQuery()
Write-Host "Root Web Title: " $ctx.Site.RootWeb.Title
Write-Host "Root Web URL: " $ctx.Site.RootWeb.Url

You can see the output like below:

Retrieve root web title and root web url using PowerShell in SharePoint Online
Retrieve root web title and root web url using PowerShell in SharePoint Online

Here we checked:

  • How to get current site title and current site URL using PowerShell in SharePoint Online
  • How to get parent web title and parent web URL using PowerShell in SharePoint Online
  • How to get root web title and root web URL using PowerShell in SharePoint Online

Retrieve list server-relative URL using PowerShell SharePoint Online

Let us see, how to retrieve list server-relative URL using PowerShell in SharePoint online.

Here I have a list name as “Logging” and here we wanted to retrieve the list server relative url.

Try{
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$SiteURL = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$ListName="Logging"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString "**********" -AsPlainText -Force
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("bijay@onlysharepoint2013.onmicrosoft.com", $securePassword)
$web = $Context.Web
$List = $web.get_lists().getByTitle($ListName)
$Context.Load($List.RootFolder)
$Context.ExecuteQuery()
$listUrl = $list.RootFolder.ServerRelativeUrl
write-host $listUrl
Retrieve web server relative url and list server relative url using PowerShell SharePoint Online
Retrieve web server relative url and list server relative url using PowerShell SharePoint Online

Retrieve web server relative URL using PowerShell SharePoint Online

Let us see, how to get web server relative URL using PowerShell SharePoint Online.

Below is the PowerShell script which will be helpful in retrieving the web server relative URL of a SharePoint online site.

Try{
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$SiteURL = "https://onlysharepoint2013.sharepoint.com/sites/bhawana"
$UserName="bijay@onlysharepoint2013.onmicrosoft.com"
$Password="*********"
Function Get-SPOCredentials([string]$UserName,[string]$Password)
{
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
}
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password
$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()
$myweb=$web.ServerRelativeURL
Write-Host $myweb

This is how to get web server relative URL using PowerShell SharePoint Online.

PowerShell SharePoint Online

We can also use PowerShell for SharePoint Online, we can use (PowerShell) SharePoint management shell to work with SharePoint site collections, sites/subsites, list or libraries etc.

SharePoint Online PowerShell commands are starting with “-SPO” like New-SPOSite

Download and Install SharePoint Online Management Shell

To work with PowerShell SharePoint Online, you need to download and install SharePoint Online management shell.

Once downloaded, install it and then you can see SharePoint Online Management Shell in your system, by using which you can connect to SharePoint Online Office 365.

You can follow below article to know step by step how to download and install SharePoint Online Management Shell.

You can also install SharePoint Online Management shell if you are using PowerShell 5 or newer versions in your system. You can run the below command in PowerShell (Administrative mode).

Install-Module -Name Microsoft.Online.SharePoint.PowerShell

Once you run the about PowerShell command, it will also install SharePoint Online Management shell.

Download SharePoint Online Management Shell
Download SharePoint Online Management Shell

Now you can run the below PowerShell commands to connect to SharePoint Online sites.

Connect-SPOService -Url https://onlysharepoint2013-admin.sharepoint.com -credential bijay@onlysharepoint2013.onmicrosoft.com
Get-SPOSite

The above PowerShell SharePoint cmdlets display all the SharePoint Online sites in Office 365.

Work with PowerShell SharePoint Online using Client Side Object Model (csom)

There is another efficient way we can work with PowerShell SharePoint Online using client side object model (csom). For this you required the below two dlls:

  • Microsoft.SharePoint.client.dll
  • Microsoft.SharePoint.client.runtime.dll

Now to work with CSOM in PowerShell SharePoint, you need to add the above SharePoint client dll’s reference at the beginning of the PowerShell script.

Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}

Note, the above path is from a SharePoint server, but if you have not installed SharePoint like in any windows 10 laptop, you download the client dlls from Nuget package and you can refer it from there like below:

Try{
Add-Type -Path 'E:\ClientDLLs\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\ClientDLLs\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}

PowerShell SharePoint Online Examples (CSOM)

Below are few PowerShell SharePoint Online examples where we are using client side object model (csom).

Cannot be loaded because the execution of scripts is disabled on this system

This PowerShell tutorial, we will discuss how to fix error cannot be loaded because the execution of scripts is disabled on this system which comes in PowerShell SharePoint.

Recently I was trying to connect to the SharePoint Online site by using Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll using PowerShell by giving references like below:

  • Add-Type -Path ‘E:\Bijay\Microsoft.SharePoint.Client.dll’
  • Add-Type -Path ‘E:\Bijay\Microsoft.SharePoint.Client.Runtime.dll’

We wrote the script using Windows PowerShell ISE. But it was giving error like: cannot be loaded because the execution of scripts is disabled on this system

cannot be loaded because the execution of scripts is disabled on this system

We were using Windows 10 64-bit laptop. To resolve the issue, Open the PowerShell command prompt in Administrator mode and then run the below command. Once the command runs successfully RESTART the laptop once.

Set-ExecutionPolicy RemoteSigned

After restart, the issue was resolved for me. This is how to fix error cannot be loaded because the execution of scripts is disabled on this system.

net System error 2240 has occurred

Let us see, how to resolve net System error 2240 has occurred The user is not allowed to log on from this workstation error in PowerShell SharePoint Online.

Recently we got one requirement to copy documents from shared drive to a SharePoint online document library using PowerShell. In that PowerShell command, we were providing the shared drvie ip address, username, and password. And then we were using the net use command like below:

$fileDirectory="\\IP Address\FolderName"
$username = "Domain\Username"
$password = "Password"
net use $fileDirectory $password /USER:$username

But in the above line it was giving the below error:

net : System error 1219 has occurred.
At C:\Users\Bijaya.Sahoo\Desktop\laregefileupload.ps1:45 char:1
+ net use $fileName $password /USER:$username
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (System error 1219 has occurred.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

The user is not allowed to log on from this workstation.

The full error looks like below:

The user is not allowed to log on from this workstation
The user is not allowed to log on from this workstation

net System error 2240 has occurred

Here the first thing you need to check is the domain name should be the same (workstation and shared drive one).

If you are using a vpn, then you can execute the code outside of the vpn.

This PowerShell SharePoint tutorial explains, how to work with PowerShell SharePoint 2013/2016 or working with PowerShell SharePoint Online.

I have explained how to download and install SharePoint Online Management Shell and work with SharePoint Online Office 365 sites, list or libraries etc.

We also saw various examples PowerShell SharePoint Online or SharePoint 2013/2016.

>