# Restoring SQL databases with multiple data files fails

**URL:** <https://forums.powershell.org/t/restoring-sql-databases-with-multiple-data-files-fails/7722>\
**Category:** PowerShell Help\
**Created:** [December 15, 2016, 2:25pm UTC](https://forums.powershell.org/t/restoring-sql-databases-with-multiple-data-files-fails/7722 "2016-12-15T14:25:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![markrdickinson](https://avatars.discourse-cdn.com/v4/letter/m/ce73a5/32.png) [@markrdickinson](https://forums.powershell.org/u/markrdickinson)\
**Post date:** [December 15, 2016, 2:25pm UTC](https://forums.powershell.org/t/restoring-sql-databases-with-multiple-data-files-fails/7722/1 "2016-12-15T14:25:59Z")

</div>

I have a process in Powershell to restore SQL databases from BAK files. Everything is working great, with one exception. If the source database has more than 1 data file the restore fails.

I know it’s the way the restore code is written, but I don’t know how to correct it to allow for multiple data (MDF, NDF) files.

Any suggestions would be appreciated! 🙂

Here’s the relevant code…

```
# Build the physical file names for the database copy
$dbfile = $fileloc + '\'+ $dbname + '_Data.mdf'
$logfile = $logloc + '\'+ $dbname + '_Log.ldf'

# Use the backup file name to create the backup device
$bdi = new-object ('Microsoft.SqlServer.Management.Smo.BackupDeviceItem') ($bckfile, 'File')

# Create the new restore object, set the database name and add the backup device
$rs = new-object('Microsoft.SqlServer.Management.Smo.Restore')
$rs.Database = $dbname
$rs.Devices.Add($bdi)

#settings for restore
$rs.NoRecovery = $false
$rs.ReplaceDatabase = $true

# Get the file list info from the backup file
$fl = $rs.ReadFileList($srv)
foreach ($fil in $fl) {
    $rsfile = new-object('Microsoft.SqlServer.Management.Smo.RelocateFile')
    $rsfile.LogicalFileName = $fil.LogicalName
    if ($fil.Type -eq 'D'){
        $rsfile.PhysicalFileName = $dbfile
        }
    else {
        $rsfile.PhysicalFileName = $logfile
        }
    $rs.RelocateFiles.Add($rsfile)
    }

# Restore the database
$rs.SqlRestore($srv)
```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 15, 2016, 4:41pm UTC](https://forums.powershell.org/t/restoring-sql-databases-with-multiple-data-files-fails/7722/2 "2016-12-15T16:41:24Z")

</div>

I can’t answer your specific question but with SQL queries (no pun intended) I usually head straight for [Chrissy Le Maire’s blog](https://blog.netnerds.net/).

I found a function that’s part of the [dbatools.io](https://dbatools.io/) project that will point you in the right direction:  
[Restore-Database.ps1 on dbatools.io](https://github.com/sqlcollaborative/dbatools/blob/master/internal/Restore-Database.ps1)

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/restoring-sql-databases-with-multiple-data-files-fails/7722/3 "2024-05-16T20:39:43Z")

</div>

Closed on May 16, 2024, 8:39pm UTC


