commands

^

Get-DbaDatabase

Author Garry Bargsley (@gbargsley), blog.garrybargsley.com , Klaas Vandenberghe (@PowerDbaKlaas) , Simone Bizzotto (@niphlod)
Availability Windows, Linux, macOS

 

Want to see the source code for this command? Check out Get-DbaDatabase on GitHub.
Want to see the Bill Of Health for this command? Check out 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

Get-DbaDatabase
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-Database] <String[]>]
    [[-ExcludeDatabase] <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
PS C:\> Get-DbaDatabase -SqlInstance localhost

Returns all databases on the local default SQL Server instance.

Example: 2
PS C:\> Get-DbaDatabase -SqlInstance localhost -ExcludeUser

Returns only the system databases on the local default SQL Server instance.

Example: 3
PS C:\> Get-DbaDatabase -SqlInstance localhost -ExcludeSystem

Returns only the user databases on the local default SQL Server instance.

Example: 4
PS C:\> 'localhost','sql2016' | Get-DbaDatabase

Returns databases on multiple instances piped into the function.

Example: 5
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress -RecoveryModel full,Simple

Returns only the user databases in Full or Simple recovery model from SQL Server instance SQL1\SQLExpress.

Example: 6
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress -Status Normal

Returns only the user databases with status 'normal' from SQL Server instance SQL1\SQLExpress.

Example: 7
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
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress,SQL2 -ExcludeDatabase model,master

Returns all databases except master and model from SQL Server instances SQL1\SQLExpress and SQL2.

Example: 9
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress,SQL2 -Encrypted

Returns only databases using TDE from SQL Server instances SQL1\SQLExpress and SQL2.

Example: 10
PS C:\> Get-DbaDatabase -SqlInstance SQL1\SQLExpress,SQL2 -Access ReadOnly

Returns only read only databases from SQL Server instances SQL1\SQLExpress and SQL2.

Example: 11
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.

Required Parameters

-SqlInstance

The target SQL Server instance or instances.

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.

Alias
Required False
Pipeline false
Default Value
-Database

Specifies one or more databases to include in the results. Supports wildcards and exact name matching.
Use this when you need to retrieve specific databases instead of all databases on the instance.

Alias
Required False
Pipeline false
Default Value
-ExcludeDatabase

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

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.

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.

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.

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.

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.

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.

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).

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.

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.

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.

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.

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.

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.

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.

Alias
Required False
Pipeline false
Default Value False