dbatools is a free PowerShell module with over 200 SQL Server administration, best practice and migration commands included.

Please note that documentation and command names may be out of date while we work furiously towards 1.0


Mount-DbaDatabase (alias Attach-DbaDatabase)

This command will attach a SQL Server database based on backup history or given file structure (see below example).

Screenshots

Mount-DbaDatabase (alias Attach-DbaDatabase)

Examples

$fileStructure = New-Object System.Collections.Specialized.StringCollection
$fileStructure.Add(“E:\archive\example.mdf”)
$filestructure.Add(“E:\archive\example.ldf”)
$filestructure.Add(“E:\archive\example.ndf”)
Mount-DbaDatabase -SqlInstance sql2016 -Database example -FileStructure $fileStructure

Will mount a database named “example” to sql2016 with the files “E:\archive\example.mdf”, “E:\archive\example.ldf”, “E:\archive\example.ndf”. The database will be given the owner “sa” and the attach option is None (as opposed to rebuilding log, enable broker, new broker, or error broker conversations)

Mount-DbaDatabase -SqlInstance sql2016 -Database example

Since the file structure was not provided, this command will attempt to determine it based on backup history. If found, a database named example will be attached to sql2016.

Mount-DbaDatabase -SqlInstance sql2016 -Database example -WhatIf

Shows what would happen if the command were executed (without actually performing the command)

Get-Help

From PowerShell, execute Get-Help Mount-DbaDatabase -Detailed for more information on this function.

Source Code

Want to see the source code? View Mount-DbaDatabase.ps1 on GitHub
 

Related commands