How to Create Multiple Subsites with Powershell From a CSV file

Sometimes it happens that you need to create Bulk subsites in a Site collection. 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.

https://j3rmeyer.wordpress.com/2015/06/22/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.

3 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!

    Like

    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.

      Like

  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.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s