Restore-sqmTableFromBackup
Backup & Restore
sqmSQLTool v1.9.244+ · Restore
✓ -WhatIf supported · original tables are never touched
Restore-sqmTableFromBackup does all of it in one call. It finds the backup chain for the requested point in time (from the msdb history, for an AlwaysOn database on all replicas, or from -BackupPath), restores it as a temporary copy <Database>_sqmTR_<timestamp> on the same instance (with STOPAT for -RestoreTime), copies each table with SELECT INTO into the target database as <table>_restore_<yyyyMMdd> and drops the copy again. Because SQL Server itself reads the backup, everything it can restore works: compressed, striped and multi-set backup files, backup encryption, TDE (the instance holds the certificate) and URL backups.
ⓘ Partial restore. If the source database uses the FULL or BULK_LOGGED recovery model, has several data filegroups and the requested tables (including LOB data and partition schemes) live in a subset of them, only
PRIMARY and those filegroups are restored (RESTORE ... WITH PARTIAL). That saves time and disk space for large databases. The filegroups are read from the live database, so a table that was dropped there always leads to a full restore. Under SIMPLE recovery SQL Server requires all read/write filegroups, so the copy is restored completely. If the partial restore fails for any reason, the function falls back to a full restore on its own.
⚠ Safety. An existing table in the target database is never overwritten, unless this function created it earlier (extended property
sqmTableRestore) and -Force is given. Before the copy database is dropped, its file paths are compared with the files of every other database on the instance; if one is shared, the copy stays and a warning is written. Before the restore, the free space per drive is checked against the file sizes in the backup.
ⓘ What is copied: all columns with data types, nullability, identity property and data; computed columns become regular columns holding their values. With
-IncludeIndexes also the primary key, unique constraints and rowstore indexes (filters and INCLUDE columns kept). Not copied: foreign keys, check and default constraints, triggers, permissions, columnstore/XML/spatial indexes. The result is a data copy for comparing and repairing rows, not a drop-in replacement for the original table.
Parameters
| Parameter | Type | Required | Default | Notes |
|---|---|---|---|---|
| -SqlInstance | string | Required | , | Instance with the source database. For an AG database any replica; the work runs on the primary. |
| -SqlCredential | PSCredential | Optional | , | SQL authentication. |
| -Database | string | Required | , | Name of the database in the backup. |
| -Table | string[] | Required | , | schema.table or table (dbo). Brackets allowed: [Sales].[Order Lines]. Several tables share one restore of the copy. |
| -RestoreTime | datetime | Optional | newest state | Point in time. With log backups exact (STOPAT); without logs the newest Full/Diff finished before it, reported in RestorePoint. |
| -BackupPath | string[] | Optional | msdb history | Backup files or folders instead of the history. Readable by the SQL Server service account; other databases in the folder are ignored. |
| -TargetDatabase | string | Optional | -Database | Database that receives the restored tables. |
| -TargetSchema | string | Optional | source schema | Must exist in the target database. |
| -Suffix | string | Optional | _restore_{yyyyMMdd} | Appended to the table name; a part in braces is a date format for the current time. |
| -IncludeIndexes | switch | Switch | $false | Primary key, unique constraints and rowstore indexes. Constraint names get the suffix (unique per schema). |
| -NoPartialRestore | switch | Switch | $false | Always restore the whole database. |
| -DataPath / -LogPath | string | Optional | instance defaults | Folders for the files of the temporary copy. |
| -SkipSpaceCheck | switch | Switch | $false | Skips the free-space check. |
| -KeepCopy | switch | Switch | $false | Keeps the copy database (name in CopyDatabase). |
| -Force | switch | Switch | $false | Replaces a target table that an earlier run of this function created. Never applies to other tables. |
| -EnableException | switch | Switch | $false | Throw instead of returning Status = 'Failed'. |
Return Value
One PSCustomObject per table: SqlInstance, SourceDatabase, SourceTable, TargetDatabase, TargetTable, Rows, IndexesCreated, RestoreMode (Full/Partial), RestorePoint, BackupSets, CopyDatabase, CopyDropped, Duration, Status (Success/Failed/WhatIf), Message.
Execution Flow
Examples
Newest state of one table, as dbo.Orders_restore_<today> in the same database
Restore-sqmTableFromBackup -SqlInstance SQL01 -Database Shop -Table dbo.Orders
State before an accidental DELETE, two tables from one restore, with primary key and indexes
Restore-sqmTableFromBackup -SqlInstance SQL01 -Database Shop `
-Table dbo.Orders, dbo.OrderLines `
-RestoreTime '2026-11-30 07:45' -IncludeIndexesGet back a table that was dropped: the copy is restored fully, the table lands next to the others
Restore-sqmTableFromBackup -SqlInstance SQL01 -Database Shop -Table dbo.Customers `
-RestoreTime '2026-11-30 16:10' -Suffix '_before_drop'Backup files from another server into a scratch database, plan only
Restore-sqmTableFromBackup -SqlInstance SQL01 -Database Shop -Table Orders `
-TargetDatabase Scratch -BackupPath '\\backup01\sql\Shop' -WhatIfRepair rows in the original table from the restored copy
$r = Restore-sqmTableFromBackup -SqlInstance SQL01 -Database Shop -Table dbo.Orders `
-RestoreTime '2026-11-30 07:45'
$r | Select-Object TargetTable, Rows, RestoreMode, RestorePoint, Status
# then, in SSMS or Invoke-DbaQuery:
# SET IDENTITY_INSERT dbo.Orders ON;
# INSERT dbo.Orders (OrderId, CustomerId, Amount)
# SELECT r.OrderId, r.CustomerId, r.Amount FROM dbo.Orders_restore_20261201 r
# WHERE NOT EXISTS (SELECT 1 FROM dbo.Orders o WHERE o.OrderId = r.OrderId);
# SET IDENTITY_INSERT dbo.Orders OFF;