Commands / Get-sqmDatabaseRestoreHistory
Get-sqmDatabaseRestoreHistory
Backup & Restore sqmSQLTool v1.8.2+ · Get ✓ -WhatIf supported
Lists every database on the instance next to its most recent restore, sourced from msdb.dbo.restorehistory via dbatools' Get-DbaDbRestoreHistory -Last. A database that has never been restored - the normal case for most production databases - is listed explicitly with an empty LastRestoreDate and RestoreType "(never restored)" rather than being silently absent, the whole point of the report is to show every database's status, not just the ones with restore history. Saves results as TXT, CSV, and HTML to the output path and returns a structured object.
Why this exists: a raw Get-DbaDbRestoreHistory -Last only returns rows for databases that actually appear in msdb.dbo.restorehistory, a database that was never restored simply never shows up. That's fine for "what happened," but useless for "did the restore I expected actually run", or for compliance/audit questions like "which of our databases have never had a restore tested." This command cross-references the full database list on the instance so the answer includes every database, not just the ones with history.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestring[]Optional$env:COMPUTERNAMEPipeline-capable. Multiple instances supported.
-SqlCredentialPSCredentialOptional,
-ExcludeDatabasestring[]Optional, Wildcards allowed. tempdb always excluded.
-IncludeSystemDatabasesswitchSwitch$falseInclude master/model/msdb. tempdb always excluded.
-OutputPathstringOptional<OutputPath config>\DatabaseRestoreHistoryCreated automatically if it does not exist.
-ContinueOnErrorswitchSwitch$falseContinue to next instance on error instead of throwing.
-EnableExceptionswitchSwitch$falseThrow exceptions immediately (overrides ContinueOnError).
-NoOpenswitchSwitch$falseDo not automatically open the generated report after creation.

Execution Flow

START dbatools installed? NO throw: dbatools not found YES -SqlInstance provided? NO → Default: $env:COMPUTERNAME Get-DbaDatabase (excl. tempdb always) -IncludeSystemDatabases: NO → also excl. master/model/msdb Get-DbaDbRestoreHistory -Last (msdb.dbo.restorehistory / backupset) most recent restore per database, keyed into a lookup table For EVERY database from Get-DbaDatabase Found in lookup → LastRestoreDate, RestoreType, RestoredBy, SourceFile Not found → RestoreType = "(never restored)", LastRestoreDate = $null (this is what a raw Get-DbaDbRestoreHistory call alone would silently omit) OutputPath exists? NO New-Item -Force create directory YES Write TXT + CSV + HTML report DatabaseRestoreHistory_InstanceName_YYYY-MM-DD.txt / .csv / .html → OutputPath Sorted: never-restored first, then by LastRestoreDate descending Return [PSCustomObject] SqlInstance · Timestamp · DetailRows · TxtFile · CsvFile · HtmlFile DetailRows: one row per database, incl. never-restored ones Instance error? -ContinueOnError: continue to next instance · else throw DONE (pipeline: next instance)

Examples

Restore history, local instance, default filters
Get-sqmDatabaseRestoreHistory
Multiple instances, include system databases
Get-sqmDatabaseRestoreHistory -SqlInstance "SQL01","SQL02" -IncludeSystemDatabases -OutputPath "D:\Reports"
Exclude a set of scratch/test databases
Get-sqmDatabaseRestoreHistory -SqlInstance "SQL01" -ExcludeDatabase "Test_*","*_scratch"
Find databases that have never been restored (e.g. for a restore-test audit)
(Get-sqmDatabaseRestoreHistory -SqlInstance "SQL01").DetailRows | Where-Object { -not $_.LastRestoreDate }
Pipeline, check all instances from OU
Get-sqmServersFromOU -OUPath "OU=SQL,DC=corp,DC=local" | Get-sqmDatabaseRestoreHistory -ContinueOnError