< Back

Get-DbaDatabase

Mon Jan 13, 2020 10:31 am

NAME Get-DbaDatabase



SYNOPSIS

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





SYNTAX

Get-DbaDatabase [-SqlInstance] <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [[-SqlCredential]

<Pscredential>] [[-Database] <System.String[]>] [[-ExcludeDatabase] <System.String[]>] [-ExcludeUser <Switch>]

[-ExcludeSystem <Switch>] [[-Owner] <System.String[]>] [-Encrypted <Switch>] [[-Status] <System.String[]>]

[[-Access] <String>] [[-RecoveryModel] <System.String[]>] [-NoFullBackup <Switch>] [[-NoFullBackupSince]

<Datetime>] [-NoLogBackup <Switch>] [[-NoLogBackupSince] <Datetime>] [-EnableException <Switch>] [-IncludeLastUsed

<Switch>] [-OnlyAccessible <Switch>] [<CommonParameters>]





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.





PARAMETERS

-Access [<String>]

Filters databases returned by their access type. Valid options for this parameter are 'ReadOnly' and

'ReadWrite'. If omitted, no filtering is performed.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Database [<System.String[]>]

Specifies one or more database(s) to process. If unspecified, all databases will be processed.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-EnableException [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Encrypted [<Switch>]

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

returned.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ExcludeDatabase [<System.String[]>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ExcludeSystem [<Switch>]

If this switch is enabled, only databases which are not System databases will be processed.



This parameter cannot be used with -ExcludeUser.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ExcludeUser [<Switch>]

If this switch is enabled, only databases which are not User databases will be processed.



This parameter cannot be used with -ExcludeSystem.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-IncludeLastUsed [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-NoFullBackup [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-NoFullBackupSince [<Datetime>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-NoLogBackup [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-NoLogBackupSince [<Datetime>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-OnlyAccessible [<Switch>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Owner [<System.String[]>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-RecoveryModel [<System.String[]>]

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

'BulkLogged'.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlCredential [<Pscredential>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]

The target SQL Server instance or instances.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Status [<System.String[]>]

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



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Tags: Database

Author: Garry Bargsley (@gbargsley), http://blog.garrybargsley.com | Klaas Vandenberghe (@PowerDbaKlaas) |

Simone Bizzotto ( @niphlod )



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



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



RELATED LINKS

https://dbatools.io/Get-DbaDatabase