Create Site Collections in Bulk from a CSV File using PowerShell in SharePoint 2016
PowerShell cmdlet New-SPSite
is used to create new site collections in SharePoint. It takes URL and
Owner mandatory parameters with other parameters. Here is an
example:
Create SharePoint Site Collection Using PowerShell :
PowerShell to Create Multiple Site Collections in Bulk from a CSV File:
Lets add some error handling and wrap New-SPSite into a reusable PowerShell function, to create multiple site collections from a CSV file.
Create SharePoint Site Collection Using PowerShell :
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue #Parameters to create new site collection $SiteName = "Demo" $SiteURL= "http://intranet.Crescent.com/sites/demo" $SiteOwner = "Crescent\SPAdmin" $SiteTemplate = "STS#0" #create site collection with given parameters New-SPSite -Url $URL -Name $SiteName -OwnerAlias $SiteOwner -Template $SiteTemplate
PowerShell to Create Multiple Site Collections in Bulk from a CSV File:
Lets add some error handling and wrap New-SPSite into a reusable PowerShell function, to create multiple site collections from a CSV file.
- Step 1: Create a CSV file in the below format. Populate the column values according to your requirements.
- Step 2: Use this PowerShell script to read from CSV and create site collections in bulk.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue #Function to Create new Site Collection Function Create-SPSite { param ( [string]$Name = $(throw "Please Provide the Site Name!"), [string]$URL = $(throw "Please Provide the Site URL!"), [string]$Owner = $(throw "Please Provide the Site Owner!"), [string]$Template = $(throw "Please Provide the Site Template!") ) Try { #Set the Error Action $ErrorActionPreference = "Stop" #Check if the site collection exists already $SiteExists = Get-SPSite | where {$_.url -eq $URL} #Check if site exists in the recycle bin $SiteExistsInRecycleBin = Get-SPDeletedSite | where {$_.url -eq $URL} If($SiteExists -ne $null) { write-host "Site $($url) exists already!" -foregroundcolor red } elseIf($SiteExistsInRecycleBin -ne $null) { write-host "Site $($url) exists in the recycle bin!" -foregroundcolor red } else { #create site collection New-SPSite -Url $URL -Name $Name -OwnerAlias $Owner -Template $Template write-host "Site Collection $($url) Created Successfully!" -foregroundcolor Green } } catch { write-host "Error: $($_.Exception.Message)" -foregroundcolor Red } Finally { #Reset the Error Action to Default $ErrorActionPreference = "Continue" } } #Read from CSV and create site collection Import-Csv "C:\SitesToCreate.csv" | Foreach-Object { Create-SPSite -Name $_.SiteName -URL $_.SiteURL -Owner $_.SiteOwner -Template $_.SiteTemplate }
Error: A parameter cannot be found that matches parameter name 'Name'.
ReplyDeleteI get the above error message on executing the script
Error: Cannot bind argument to parameter 'Url' because it is an empty string.
ReplyDeleteI get the error message when execute the script
That means, The URL parameter you passed to the function is null! Just make sure your CSV file has exactly the same column names and has valid URLs in it!
Delete