---
title: "Get-DbaLastBackup"
description: "Retrieves last backup dates and times for database backup compliance monitoring"
url: "https://dbatools.io/Get-DbaLastBackup/"
availability: "Windows, Linux, macOS"
tags: ["DisasterRecovery", "Backup"]
author: "Klaas Vandenberghe (@PowerDBAKlaas)"
source: "https://github.com/dataplat/dbatools/blob/master/public/Get-DbaLastBackup.ps1"
last_updated: "2024-01-01"
---

# Get-DbaLastBackup

## Synopsis

Retrieves last backup dates and times for database backup compliance monitoring

## Description

Queries msdb backup history to retrieve the most recent full, differential, and transaction log backup dates for each database. This function helps DBAs quickly identify backup gaps and verify compliance with backup policies by showing when each backup type was last performed. The function also calculates elapsed time since each backup and provides status indicators to highlight potential issues, such as databases with no recent backups or transaction log backups that are overdue in full recovery model databases. Default output includes Server, Database, LastFullBackup, LastDiffBackup, and LastLogBackup columns.

## Syntax

```powershell
Get-DbaLastBackup
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-Database] <Object[]>]
    [[-ExcludeDatabase] <Object[]>]
    [-ExcludeReplica]
    [-EnableException]
    [<CommonParameters>]

```

## Examples

### Example 1: Returns a custom object with Server name, Database name, and the date the last time backups were performed

```powershell
PS C:\> Get-DbaLastBackup -SqlInstance ServerA\sql987
```

### Example 2: Returns a custom object with Server name, Database name, and the date the last time backups were performed...

```powershell
PS C:\> Get-DbaLastBackup -SqlInstance ServerA\sql987 | Select-Object *
```

Returns a custom object with Server name, Database name, and the date the last time backups were performed, and also recoverymodel and calculations on how long ago backups were taken and what the  
status is.  

### Example 3: Returns a gridview displaying ComputerName, InstanceName, SqlInstance, Database, RecoveryModel...

```powershell
PS C:\> Get-DbaLastBackup -SqlInstance ServerA\sql987 | Select-Object * | Out-Gridview
```

Returns a gridview displaying ComputerName, InstanceName, SqlInstance, Database, RecoveryModel, LastFullBackup, LastDiffBackup, LastLogBackup, SinceFull, SinceDiff, SinceLog,  
LastFullBackupIsCopyOnly, LastDiffBackupIsCopyOnly, LastLogBackupIsCopyOnly, DatabaseCreated, DaysSinceDbCreated, Status  

### Example 4: Returns all databases on the given instances without a full backup in the last three days

```powershell
PS C:\> $MyInstances | Get-DbaLastBackup | Where-Object -FilterScript { $_.LastFullBackup.Date -lt (Get-Date).AddDays(-3) } | Format-Table -Property SqlInstance, Database, LastFullBackup
```

Returns all databases on the given instances without a full backup in the last three days.  
Note that the property LastFullBackup is a custom object, with the subproperty Date of type datetime and therefore suitable for comparison with dates.  

### Example 5: Filters for the databases that had a copy_only full backup done as the last backup

```powershell
PS C:\> Get-DbaLastBackup -SqlInstance ServerA\sql987 | Where-Object { $_.LastFullBackupIsCopyOnly -eq $true }
```

### Example 6: Returns last backup information for all databases on both instances, excluding availability group databases...

```powershell
PS C:\> Get-DbaLastBackup -SqlInstance sql2019, sql2019b -ExcludeReplica
```

Returns last backup information for all databases on both instances, excluding availability group databases  
where the current instance is not the preferred backup replica. This prevents false alerts when secondary  
replicas appear to have no recent backups because backups are only performed on the preferred replica.  

### Required Parameters

##### -SqlInstance

The target SQL Server instance or instances.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | True |
| Pipeline | true (ByValue) |
| Default Value |  |

### Optional Parameters

##### -SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).  
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.  
For MFA support, please use Connect-DbaInstance.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Database

Specifies which databases to check for backup history. Accepts wildcards for pattern matching.  
Use this when you need to focus on specific databases rather than scanning all databases on the instance.  
Helpful for monitoring critical production databases or troubleshooting backup issues on particular databases.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -ExcludeDatabase

Excludes specific databases from the backup history check. Commonly used to skip system databases or test databases.  
Use this when you want to check most databases but exclude certain ones like tempdb, development databases, or databases with known backup exemptions.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -ExcludeReplica

When this switch is enabled, databases in an AlwaysOn Availability Group where the current SQL Server instance  
is not the preferred backup replica are excluded from the results.  
This is useful when running Get-DbaLastBackup against multiple servers in an availability group to avoid  
false positives where secondary replicas appear to have missing backups because backups are taken on the preferred replica only.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.  
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.  
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

## Outputs

**PSCustomObject**

Returns one object per database processed, containing backup status information and compliance metrics.

**Default display properties (via Select-DefaultView):**

- ComputerName: The computer name of the SQL Server instance
- InstanceName: The SQL Server instance name
- SqlInstance: The full SQL Server instance name (computer\instance)
- Database: The name of the database
- LastFullBackup: DateTime of the most recent full backup (DbaDateTime object with Date subproperty)
- LastDiffBackup: DateTime of the most recent differential backup (DbaDateTime object with Date subproperty)
- LastLogBackup: DateTime of the most recent transaction log backup (DbaDateTime object with Date subproperty)

**Additional properties available (use Select-Object * to display):**

- RecoveryModel: Database recovery model (Simple, Full, or BulkLogged)
- SinceFull: DbaTimeSpan representing time elapsed since last full backup; null if never backed up
- SinceDiff: DbaTimeSpan representing time elapsed since last differential backup; null if never backed up
- SinceLog: DbaTimeSpan representing time elapsed since last transaction log backup; null if never backed up
- LastFullBackupIsCopyOnly: Boolean indicating if the last full backup was copy-only
- LastDiffBackupIsCopyOnly: Boolean indicating if the last differential backup was copy-only (always false per SQL Server rules)
- LastLogBackupIsCopyOnly: Boolean indicating if the last transaction log backup was copy-only
- DatabaseCreated: DateTime when the database was created
- DaysSinceDbCreated: Integer number of days since database creation
- Status: String status indicator - "OK", "New database, not backed up yet", "No Full or Diff Back Up in the last day", or "No Log Back Up in the last hour"
The LastFullBackup, LastDiffBackup, and LastLogBackup properties are DbaDateTime objects that can be compared with .Date subproperty.

---

Part of [dbatools](https://dbatools.io/), a free and open source PowerShell module for SQL Server administration. Full command index: https://dbatools.io/commands/
