How to Create Multiple Subsites with Powershell From a CSV file

Sometimes it happens that you need to create Bulk subsites in a Sitecollection. And there is one thing what it-ers don’t like and that is manual work.

With this PowerShell script you can use a EXCEL.CSV to import as many sites as you want.

Step1

First of all you need to install the SharePoint2013 Client Components SDK.

http://www.microsoft.com/en-au/download/details.aspx?id=35585

Step2

The next step is to start up powershell ISE as a Administrator.

Now we need to make a connection to you Office365 admin Tenant with the following command

connect-SPOservice https://tenantname.admin.sharepoint.com

subsite1

Fill in your Office365 administrator credentials and press OK.

Now you are connected to you SharePoint tenant.

Step 3

And we can start editing the script to import the CSV.

Copy the following script in your Powershell script pane.

$csvLocation = “C:\Installt\sites.csv
$template = “template
$siteUrl = “https://tenant.sharepoint.com/sites/sitecollection
$username = “youraccount@tenant.nl”#yourpassword
Add-Type -Path “c:\Microsoft.SharePoint.Client.dll”
Add-Type -Path “c:\Microsoft.SharePoint.Client.Runtime.dll”

$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
$csv = Import-Csv $csvLocation

ForEach ($site in $csv) {
$site = $($site.collumname of CSV)
$webCreationInformation = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$webCreationInformation.Url = $site
$webCreationInformation.Title = $site
$webCreationInformation.WebTemplate = $template
$newWeb = $ctx.Web.Webs.Add($webCreationInformation)
$newWeb.ResetRoleInheritance()
$ctx.Load($newWeb)
$ctx.ExecuteQuery()

Write-Host “Site created:” $newWeb.Title ” at: ” $siteUrl “/” $newWeb.Url
}

Now you probably wonder what you need top fill in at Template. You can choos a custom template or you can choose one of the template  from the site bellow.

SharePoint online Template codes overview

In the next couple of days i will create another blog about this subject where i show you how to create a Custom template and how you can get the template code of your custom template.

Good luck and don’t hesitate to ask me question about this subject.

Please follow and like us:

6 thoughts on “How to Create Multiple Subsites with Powershell From a CSV file

  1. Thanks for this! Nice and straightforward but I have a few queries:

    Line 4, do you actually need to replace #password with my value? It is gathered by a prompt later (line 7)

    Line 13, what is meant by: $($site.collumname of CSV) what must I insert there? It seems to gather info from the columns in the lower lines.

    Thanks in advance!

    1. Sorry for my late response. You actually answer your own questions. The first logon is for the logon to the admin tenant of office 365. After this part you get the prompt to logon to the SharePoint online tenant itself.

      $site.collumname of CSV means that you have to put in the name of the collum in your CSV file. Like
      $site.customernumbers

      Cheers for reading my blog.

  2. Hi,

    In your script where I can put my Excel CSV file column. I have 2 columns in my Excel sheet, one for Title and second for URL. How to adjust both with your script, Please suggest.

  3. Good Morning,

    Following the tutorial and wondering if I missed a previous tut on how to create the Excel CSV that will create the subsites. My apologies if I am overlooking something obvious.

  4. Think I figured out my last question. But now I am getting error:

    Exception calling “ExecuteQuery” with “0” argument(s): “Identity Client Runtime Library (IDCRL) did not get a response from the Login server.”
    At C:\scripts\MultiSubSites365.ps1:23 char:2
    + $ctx.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IdcrlException

    Here is how my column looks in the CSV
    SubsiteName ( This is my column name—is this correct format?)
    SampleSite1
    SampleSite2
    SampleSite3

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.