Content DB Attach using PowerShell

In this post, I’ll talk about how to attach\mount a SharePoint content database to a SharePoint web application after it has been restored\attached to the SQL server. See my previous post for details on how to restore a database using PowerShell.

SharePoint provides a cmdlet to do the content db attach. This cmdlet is called Mount-SPContentDatabase. I will create a script block wrapper for this cmdlet and show how to call it remotely. The script block below adds the SharePoint PowerShell snapin if it’s not already added, selects a web application in the farm, then calls the Mount-SPContentDatabase to do the content db attach.

[ScriptBlock] $global:AttachSPContentDB =
    param([string] $dbName, [string] $dbServer)

        # Load the Sharepoint Cmdlets
        Write-Host "Loading SharePoint PowerShell Snapin"
        $spSnapin = Get-PSSnapin | where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}
        if($spSnapin -eq $null)
            Add-PSSnapin Microsoft.SharePoint.PowerShell
            Write-Host -ForegroundColor "Green" -Object "SharePoint PowerShell Snapin loaded"
            Write-Host -ForegroundColor "Gray" -Object "SharePoint PowerShell Snapin already loaded"

        # Get  web applciation url
        Write-Host "Selecting a web application in the farm"
        $webAppUrl = (Get-SPWebApplication | select -Index 0).Url

        # Mount the database at the specified SQL server to the web application
        Write-Host "Mounting database '$dbName' at SQL server '$dbServer' to web application '$webAppUrl'"
        $newDB = Mount-SPContentDatabase $dbName -DatabaseServer $dbServer -WebApplication $webAppUrl -AssignNewDatabaseId
        Write-Host -ForegroundColor "Green" -Object "Database mounted successfully with ID: " + ($newDB.Id)

        # Return content DB Id
        return $newDB.Id.ToString()
    catch [Exception]
        Write-Host -ForegroundColor "Red" -Object $_.Exception

        return $null

To call this script block, pass the name of the Sql server and the name of the content database. Make sure that the content database is already restored\attached to the SQL server.

.$AttachSPContentDB "ContentDBName" "SqlServerName"

To call the script block on a remote SharePoint WFE, you can simply run

Invoke-Command -ComputerName "WFEName" -ScriptBlock { .$AttachSPContentDB "ContentDBName" "SqlServerName" }

Note that the New-SPContentDatabase cmdlet has the same effect as Mount-SPContentDatabase. The only difference is that New-SPContentDatabase creates a new SQL database if there is no database with the same name already available on the SQL server.


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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