Invoke-sqmRestoreDatabase
Backup & Restore
sqmSQLTool v1.8.2+ · Invoke
✓ -WhatIf supported
AlwaysOn: You can run this on any replica — if the database is in an AG, the function automatically detects the primary and routes all AG operations (remove, rejoin) through it. The only hard stop: if the database is in an AG and
-KeepAlwaysOn is set, the function aborts, because a restore requires removing the database from the AG first.Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Optional | $env:COMPUTERNAME | Target instance. Default: local machine. |
| -SqlCredential | PSCredential | Optional | — | SQL login credentials. |
| -BackupFile | string[] | Required* | — | Single .bak file or striped backup. Use instead of -BackupFiles. |
| -BackupFiles | string[] | Required* | — | Ordered sequence: Full, [Diff], [Log1], [Log2]… |
| -DatabaseName | string | Required | — | Name of the database in the backup. |
| -NewDatabaseName | string | Optional | — | Rename database after restore. |
| -NewDatabaseFilePath | string | Optional | Instance default data path | Target directory for .mdf / .ndf files. |
| -NewLogFilePath | string | Optional | Instance default log path | Target directory for .ldf file. |
| -BackupBeforeRestore | switch | Switch | $false | Full backup of existing DB before restore. Skipped if DB is in AG. |
| -NoUserExport | switch | Switch | $false | Skip user export/import. Users are exported by default. |
| -KeepAlwaysOn | switch | Switch | $false | Do NOT remove from AG. Only valid if DB is already outside AG — otherwise aborts. |
| -WithNoRecovery | switch | Switch | $false | All restores with NORECOVERY. DB stays in restoring state. |
| -ContinueWithNoRecovery | switch | Switch | $false | Last restore in sequence also with NORECOVERY. |
| -ForceSingleUser | switch | Switch | $false | Force SINGLE_USER even if no active connections detected. |
| -RejoinAvailabilityGroup | switch | Switch | $false | Re-add to AG after restore (Automatic Seeding). Requires Automatic Seeding on the AG. |
* Either -BackupFile or -BackupFiles is required (mutually exclusive parameter sets).
Execution Flow
Examples
Simple restore — local instance, full backup
Invoke-sqmRestoreDatabase -BackupFile "D:\Backup\AdventureWorks.bak" -DatabaseName "AdventureWorks"
AlwaysOn: restore and rejoin AG automatically
Invoke-sqmRestoreDatabase `
-SqlInstance "SQL01" `
-BackupFile "D:\Backup\MyDB.bak" `
-DatabaseName "MyDB" `
-BackupBeforeRestore `
-RejoinAvailabilityGroupFull + Diff + Log restore sequence (point-in-time)
Invoke-sqmRestoreDatabase `
-SqlInstance "SQL01" `
-BackupFiles @(
"D:\Backup\MyDB_Full.bak",
"D:\Backup\MyDB_Diff.bak",
"D:\Backup\MyDB_Log1.trn",
"D:\Backup\MyDB_Log2.trn"
) `
-DatabaseName "MyDB" `
-NewDatabaseName "MyDB_Test" `
-NewDatabaseFilePath "E:\SQLData" `
-NewLogFilePath "F:\SQLLog"NORECOVERY — keep DB in restoring state for more logs
Invoke-sqmRestoreDatabase -SqlInstance "SQL01" -BackupFile "D:\Backup\MyDB.bak" -DatabaseName "MyDB" -WithNoRecovery