Create Site Collection in New Content Database Script

11/12/2010

PowerShell

Update: Post now resides at new blog site! http://www.tcscblog.com/2010/12/28/create-site-collection-in-new-content-database-script/

In 2007 you had the stsadm command createsiteinnewdb. However, there is no PowerShell equivalent command in 2010. So, I created a script that performs the same action. It takes in input, creates a new site collection in a new content database, then sets the max site count to 1 and warning site count to 0 to lock it down from any other site collection being added to the database. This works great for scenarios where we build one site collection per department, division, etc.

Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue
$server = Read-Host “Enter SQL Server”
$dbname = Read-Host “Enter Database Name”
$webapp = Read-Host “Enter Web Application URL”
$site = Read-Host “Enter New Site Collection URL”
$owner1 = Read-Host “Enter Primary Site Collection Admin”
$owner2 = Read-Host “Enter Secondary Site Collection Admin”
New-SPContentDatabase -Name $dbname -DatabaseServer $server -WebApplication $webapp | out-null
New-SPSite -URL $site -OwnerAlias $owner1 -SecondaryOwnerAlias $owner2 -ContentDatabase $dbname | out-null
Get-SPContentDatabase -Site $site | Set-SPContentDatabase -MaxSiteCount 1 -WarningSiteCount 0
Write-Host ” ”
Write-Host “Site Collection at” $site “has been created in the” $dbname “content database” -ForegroundColor Yellow

When you access the site you are then prompted to select the appropriate template.

_____________________

Sponsors *Trusted SharePoint Ads Only!*

Packaged Enterprise Search
for SharePoint.View a quick online demo here! Surfray.com/Ontolica

Free Outlook Sidebar!
Access SharePoint from Outlook w/ harmon.ie Sidebar. Download Now!

SharePoint Admin Toolkit – Download now!
FREE Tools to Simplify SharePoint Management

SharePoint Migration Tools
Sharegate: No-Brainer Migration Tools for SharePoint. $375.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.

8 Tips to Increase SharePoint Adoption
Download Free Whitepaper

Advertisements

Subscribe

Subscribe to our RSS feed and social profiles to receive updates.

15 Comments on “Create Site Collection in New Content Database Script”

  1. uzair Says:

    Hi, will this script also work if i want to create host named site collections?

    Reply

  2. Gerald Says:

    Thanks for the great example, it’s helped allot… though I’m stuck with a migration issue… I have a 2007 web application with multiple SC’s in thier own DB’s … how can these multiple content DB’s be migrated to 2010 using the attach/detach approach ?

    Here’s the basic structure
    http://intranet – intranet_content
    http://intranet/departments/legal – intranet_legal_content
    http://intranet/departments/sales – intranet_sales_content
    http://intranet/departments/security – intranet_security_content

    tia
    Gerald

    Reply

    • sptwentyten Says:

      Glad to hear that the script helped!

      You backup each DB and move it/restore it to the SQL Server that is allocated for SharePoint 2010. Once there, you can perform Test-SPContentDatabase against the restored DBs to determine their readiness for migration. Once all issues/missing features etc have been resolved, you can run Mount-SPContentDatabase to attach the database to SharePoint 2010 which kicks off the upgrade. You would need to do your Intranet_Content DB first since I’m assuming that is where the Root Site Collection is stored. Do a search for these two PowerShell commands as well as SharePoint 2010 upgrade and you will find tons of info! Hope that helps and check out http://www.tcsc.com, my employer, should you need any assistance with this. I handle most of the SharePoint migations and upgrades here.

      Reply

      • Gerald Says:

        Thanks for the quick reply, I am already using powershell to migrate several of my web applications using the commands you’ve suggested… I can migrate the primary site no isssue.. but where it gets confusing, I re-created all of the managed paths within the primary web application, then when re-creating the SC’s how can I specify the 2007 content database ? or is there another apporach I’m missing ?

        tia
        Gerald

      • sptwentyten Says:

        You do not need to “re-create” a SC and point it to the 2007 content database. You use mount-spcontentdatabase and point it to the primary web app. If your managed paths are already setup, then once the upgrade has completed then your SC will just be there…no need to create anything. Perhaps I’m misunderstanding you though…

  3. Gerald Says:

    That’s what I was hoping, but no such luck… unless I’m doing something incorrect when I build the web application, if I don’t re-create the managed paths and SC’s nothing appears in the migrated web application… here’s the steps I’m taking…

    back up and restore all my 2007 content db’s to the new SQL server…

    #build web app
    new-spwebapplication -name $webapp_name -Port $webapp_port -HostHeader $webapp_hostheader -URL $webapp_url -ApplicationPool $WebAppPoolName -ApplicationPoolAccount $appPoolAccount -DatabaseName $WebAppDB -DatabaseServer $databaseServerName

    #build SC
    new-SPSite -url $webapp_url -OwnerAlias $sc_owner -Language $sc_language -Template $sc_template -Name $webapp_name

    #remove the temporary content DB
    Get-SPContentDatabase -WebApplication $webapp_url | Remove-SPContentDatabase -confirm:$false

    #mount the primary 2007 content DB
    Mount-SPContentDatabase $WebAppConvertDB -webapplication $webapp_url

    after this step there are no SC’s or managed paths listed in CA, if I mount the additional content databases with out any managed paths or SC’s they are just there… un-usable…

    Thanks
    Gerald

    Reply

    • sptwentyten Says:

      After Build Web App, do you go in through the UI on CA and create the appropriate managed paths? I’ve heard issues where you need to create the managed paths yourself first before doing Mount-SPContentDatabase.

      If you go to CA and under Upgrade and Migration > Upgrade Status does it say successful or failed? What do the logs say? The log location can be found at the same Upgrade Status page.

      If that doesn’t work, build the web app, SC, remove temp DB all through the UI…only do Mount-SPContentDatabase through PowerShell and see what that gets you.

      Reply

  4. fabian williams Says:

    Pretty good script mate… Im using it!

    Reply

  5. Matt Dillard Says:

    Awesome script! Thanks!!

    Reply

  6. Sidd Says:

    Thank you for the script.. It worked for me as well…

    Reply

Trackbacks/Pingbacks

  1. promovare prin facebook - 12/30/2011

    promovare prin facebook…

    […]Create Site Collection in New Content Database Script « SP2010 Blog[…]…

  2. PowerShell Commands for Real-World SharePoint | ProActive Speaks - 04/27/2012

    […] is actually a very good article on this subject by SharePoint Adam. In this article, he explains how to create an explicitly managed […]

  3. Dynamics CRM & SharePoint 2010 Integration – Useful Links | daveymcglade.net - 08/17/2012

    […] I’ve opted for the latter, as creating a site collection up front through PowerShell with it’s own content database is relatively straightforward. Do this for the estimated number of years worth of sites you need and you’re good to go. Again, this excellent guide helped me out Create Site Collection in New Content Database Script […]

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: