commands

^

Get-DbaDatabase

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

 

Synopsis

Gets SQL Database information for each database that is present on the target instance(s) of SQL Server.

Description

The Get-DbaDatabase command gets SQL database information for each database that is present on the target instance(s) of SQL Server. If the name of the database is provided, the command will return only the specific database information.

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 database(s) to process. If unspecified, all databases will be processed.

Alias
Required False
Pipeline false
Default Value
-ExcludeDatabase

Specifies one or more database(s) to exclude from processing.

Alias
Required False
Pipeline false
Default Value
-ExcludeUser

If this switch is enabled, only databases which are not User databases will be processed. This parameter cannot be used with -ExcludeSystem.

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

If this switch is enabled, only databases which are not System databases will be processed. This parameter cannot be used with -ExcludeUser.

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

Specifies one or more database owners. Only databases owned by the listed owner(s) will be returned.

Alias
Required False
Pipeline false
Default Value
-Encrypted

If this switch is enabled, only databases which have Transparent Data Encryption (TDE) enabled will be returned.

Alias
Required False
Pipeline false
Default Value False
-Status

Specifies one or more database statuses to filter on. Only databases in the status(es) listed will be returned. Valid options for this parameter are 'Emergency', 'Normal', 'Offline', 'Recovering', 'Restoring', 'Standby', and 'Suspect'.

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

Filters databases returned by their access type. Valid options for this parameter are 'ReadOnly' and 'ReadWrite'. If omitted, no filtering is performed.

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

Filters databases returned by their recovery model. Valid options for this parameter are 'Full', 'Simple', and 'BulkLogged'.

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

If this switch is enabled, only databases without a full backup recorded by SQL Server will be returned. This will also indicate which of these databases only have CopyOnly full backups.

Alias
Required False
Pipeline false
Default Value False
-NoFullBackupSince

Only databases which haven't had a full backup since the specified DateTime will be returned.

Alias
Required False
Pipeline false
Default Value
-NoLogBackup

If this switch is enabled, only databases without a log backup recorded by SQL Server will be returned. This will also indicate which of these databases only have CopyOnly log backups.

Alias
Required False
Pipeline false
Default Value False
-NoLogBackupSince

Only databases which haven't had a log backup since the specified DateTime will be returned.

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

If this switch is enabled, the last used read & write times for each database will be returned. This data is retrieved from sys.dm_db_index_usage_stats which is reset when SQL Server is restarted.

Alias
Required False
Pipeline false
Default Value False
-OnlyAccessible

If this switch is enabled, only accessible databases are returned (huge speedup in SMO enumeration)

Alias
Required False
Pipeline false
Default Value False

 

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.