---
title: "Get-DbaDatabase"
description: "Retrieves database objects and metadata from SQL Server instances with advanced filtering and usage analytics."
url: "https://dbatools.io/Get-DbaDatabase/"
availability: "Windows, Linux, macOS"
tags: ["Database"]
author: "Garry Bargsley (@gbargsley), blog.garrybargsley.com | Klaas Vandenberghe (@PowerDbaKlaas) | Simone Bizzotto (@niphlod)"
source: "https://github.com/dataplat/dbatools/blob/master/public/Get-DbaDatabase.ps1"
last_updated: "2024-01-01"
---

# Get-DbaDatabase

## Synopsis

Retrieves database objects and metadata from SQL Server instances with advanced filtering and usage analytics.

## Description

Retrieves detailed database information from one or more SQL Server instances, returning rich database objects instead of basic metadata queries.  
This command provides comprehensive filtering options for database status, access type, recovery model, backup history, and encryption status, making it essential for database inventory, compliance auditing, and maintenance planning.  
Unlike querying sys.databases directly, this returns full SMO database objects with calculated properties for backup status, usage statistics from DMVs, and consistent formatting across SQL Server versions.  
Supports both on-premises SQL Server (2000+) and Azure SQL Database with automatic compatibility handling.

## Syntax

```powershell
Get-DbaDatabase
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-Database] <String[]>]
    [[-ExcludeDatabase] <String[]>]
    [[-Pattern] <String[]>]
    [-ExcludeUser]
    [-ExcludeSystem]
    [[-Owner] <String[]>]
    [-Encrypted]
    [[-Status] <String[]>]
    [[-Access] <String>]
    [[-RecoveryModel] <String[]>]
    [-NoFullBackup]
    [[-NoFullBackupSince] <DateTime>]
    [-NoLogBackup]
    [[-NoLogBackupSince] <DateTime>]
    [-EnableException]
    [-IncludeLastUsed]
    [-OnlyAccessible]
    [<CommonParameters>]

```

## Examples

### Example 1: Returns all databases on the local default SQL Server instance

```powershell
PS C:\> Get-DbaDatabase -SqlInstance localhost
```

### Example 2: Returns only the system databases on the local default SQL Server instance

```powershell
PS C:\> Get-DbaDatabase -SqlInstance localhost -ExcludeUser
```

### Example 3: Returns only the user databases on the local default SQL Server instance

```powershell
PS C:\> Get-DbaDatabase -SqlInstance localhost -ExcludeSystem
```

### Example 4: Returns databases on multiple instances piped into the function

```powershell
PS C:\> 'localhost','sql2016' | Get-DbaDatabase
```

### Example 5: Returns only the user databases in Full or Simple recovery model from SQL Server instance SQL1\SQLExpress

```powershell
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress -RecoveryModel full,Simple
```

### Example 6: Returns only the user databases with status &#39;normal&#39; from SQL Server instance SQL1\SQLExpress

```powershell
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress -Status Normal
```

### Example 7: Returns the databases from SQL Server instance SQL1\SQLExpress and includes the last used information from...

```powershell
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress -IncludeLastUsed
```

Returns the databases from SQL Server instance SQL1\SQLExpress and includes the last used information  
from the sys.dm_db_index_usage_stats DMV.  

### Example 8: Returns all databases except master and model from SQL Server instances SQL1\SQLExpress and SQL2

```powershell
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress,SQL2 -ExcludeDatabase model,master
```

### Example 9: Returns only databases using TDE from SQL Server instances SQL1\SQLExpress and SQL2

```powershell
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress,SQL2 -Encrypted
```

### Example 10: Returns only read only databases from SQL Server instances SQL1\SQLExpress and SQL2

```powershell
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress,SQL2 -Access ReadOnly
```

### Example 11: Returns databases &#39;OneDb&#39; and &#39;OtherDB&#39; from SQL Server instances SQL2 and SQL3 if databases by those names...

```powershell
PS C:\> Get-DbaDatabase -SqlInstance SQL2,SQL3 -Database OneDB,OtherDB
```

Returns databases 'OneDb' and 'OtherDB' from SQL Server instances SQL2 and SQL3 if databases by those names exist on those instances.  

### Example 12: Returns all databases that match the regex pattern &quot;^dbatools_&quot; (e.g., dbatools_example1, dbatools_example2)...

```powershell
PS C:\> Get-DbaDatabase -SqlInstance SQL2,SQL3 -Pattern "^dbatools_"
```

Returns all databases that match the regex pattern "^dbatools_" (e.g., dbatools_example1, dbatools_example2) from SQL Server instances SQL2 and SQL3.  

### 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 one or more databases to include in the results using exact name matching.  
Use this when you need to retrieve specific databases instead of all databases on the instance.

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

##### -ExcludeDatabase

Specifies one or more databases to exclude from the results using exact name matching.  
Use this to filter out specific databases like test or staging environments from your inventory.

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

##### -Pattern

Specifies a pattern for filtering databases using regular expressions.  
Use this when you need to match databases by pattern, such as "^dbatools_" or ".*_prod$".  
This parameter supports standard .NET regular expression syntax.

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

##### -ExcludeUser

Returns only system databases (master, model, msdb, tempdb).  
Use this when you need to focus on system database maintenance tasks or validation.  
This parameter cannot be used with -ExcludeSystem.

| Property | Value |
| --- | --- |
| Alias | SystemDbOnly,NoUserDb,ExcludeAllUserDb |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -ExcludeSystem

Returns only user databases, excluding system databases (master, model, msdb, tempdb).  
Use this when you need to focus on application databases for maintenance, backup, or compliance reporting.  
This parameter cannot be used with -ExcludeUser.

| Property | Value |
| --- | --- |
| Alias | UserDbOnly,NoSystemDb,ExcludeAllSystemDb |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -Owner

Filters databases by their database owner (the principal listed as the database owner).  
Use this to find databases owned by specific accounts for security auditing or ownership cleanup.  
Accepts login names like 'sa', 'DOMAIN\user', or service account names.

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

##### -Encrypted

Returns only databases with Transparent Data Encryption (TDE) enabled.  
Use this for compliance reporting or to verify which databases have encryption configured for data protection.

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

##### -Status

Filters databases by their current operational status. Returns only databases matching the specified status values.  
Use this to identify databases requiring attention (Suspect, Offline) or in specific states for maintenance planning.  
Valid options: EmergencyMode, Normal, Offline, Recovering, RecoveryPending, Restoring, Standby, Suspect.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | @('EmergencyMode', 'Normal', 'Offline', 'Recovering', 'RecoveryPending', 'Restoring', 'Standby', 'Suspect') |
| Accepted Values | EmergencyMode,Normal,Offline,Recovering,RecoveryPending,Restoring,Standby,Suspect |

##### -Access

Filters databases by their read/write access mode. Returns only databases set to the specified access type.  
Use ReadOnly to find reporting databases or those temporarily set to read-only for maintenance.  
Valid options: ReadOnly, ReadWrite.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |
| Accepted Values | ReadOnly,ReadWrite |

##### -RecoveryModel

Filters databases by their recovery model setting, which controls transaction log behavior and backup capabilities.  
Use this to verify recovery model consistency or find databases needing model changes for backup strategy compliance.  
Valid options: Full (point-in-time recovery), Simple (no log backups), BulkLogged (minimal logging for bulk operations).

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | @('Full', 'Simple', 'BulkLogged') |
| Accepted Values | Full,Simple,BulkLogged |

##### -NoFullBackup

Returns only databases that have never had a full backup or only have CopyOnly full backups recorded in msdb.  
Use this to identify databases at risk due to missing backup coverage for disaster recovery planning.

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

##### -NoFullBackupSince

Returns databases that haven't had a full backup since the specified date and time.  
Use this to identify databases with stale backups that may violate your backup policy or RTO requirements.

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

##### -NoLogBackup

Returns databases in Full or BulkLogged recovery model that have never had a transaction log backup.  
Use this to identify databases where transaction logs may be growing unchecked due to missing log backup strategy.

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

##### -NoLogBackupSince

Returns databases that haven't had a transaction log backup since the specified date and time.  
Use this to find databases with overdue log backups that may cause transaction log growth or RPO violations.

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

##### -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 |

##### -IncludeLastUsed

Adds LastRead and LastWrite columns showing when databases were last accessed based on index usage statistics.  
Use this to identify unused or rarely accessed databases for decommissioning or archival decisions.  
Data is retrieved from sys.dm_db_index_usage_stats and resets when SQL Server restarts.

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

##### -OnlyAccessible

Returns only databases that are currently accessible, excluding offline or inaccessible databases.  
Use this to improve performance when you only need databases that can be queried, providing significant speedup for SMO enumeration.

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

## Outputs

**Microsoft.SqlServer.Management.Smo.Database**

Returns one SMO Database object for each database on the specified instances matching the filter criteria.

**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)
- Name: Database name
- Status: Current database status (EmergencyMode, Normal, Offline, Recovering, RecoveryPending, Restoring, Standby, Suspect)
- IsAccessible: Boolean indicating if the database is currently accessible
- RecoveryModel: Database recovery model (Full, Simple, BulkLogged)
- LogReuseWaitStatus: Status of transaction log reuse (LogSwitch, ChkptBkup, ActiveBkup, ActiveTran, etc.)
- Size: Database size in megabytes (MB)
- Compatibility: Database compatibility level (numeric value representing SQL Server version)
- Collation: Database collation setting
- Owner: Database owner login name
- Encrypted: Boolean indicating if Transparent Data Encryption (TDE) is enabled
- LastFullBackup: DateTime of the most recent full backup
- LastDiffBackup: DateTime of the most recent differential backup
- LastLogBackup: DateTime of the most recent transaction log backup

**When -NoFullBackup or -NoFullBackupSince is specified, an additional property is included:**

- BackupStatus: String indicating backup state (e.g., "Only CopyOnly backups", $null for normal backups)

**When -IncludeLastUsed is specified, additional properties are included:**

- LastIndexRead: DateTime of last read operation from sys.dm_db_index_usage_stats
- LastIndexWrite: DateTime of last write operation from sys.dm_db_index_usage_stats

**Additional properties available (from SMO Database object):**

- IsCdcEnabled: Boolean indicating if Change Data Capture is enabled (SQL Server 2008+)
- And all other standard SMO Database properties (use Select-Object * to see all)
All properties from the base SMO Database object are accessible via Select-Object even though only default properties are displayed without using the -Property parameter.

---

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