Thor Logo dbatools

Find-DbaDatabase

View Source
Stephen Bennett, sqlnotesfromtheunderground.wordpress.com
Windows, Linux, macOS

Synopsis

Searches multiple SQL Server instances for databases matching name, owner, or Service Broker GUID patterns

Description

Performs database discovery and inventory across multiple SQL Server instances by searching for databases that match specific criteria. You can search by database name (using regex patterns), database owner, or Service Broker GUID to locate databases across environments.

This is particularly useful for tracking databases across development, test, and production environments, finding databases by ownership for security audits, or identifying databases with matching Service Broker GUIDs. The function returns detailed information including database size, object counts (tables, views, stored procedures), and creation details.

Service Broker GUIDs can become mismatched on restored databases when using ALTER DATABASE…NEW_BROKER or when Service Broker is disabled, which resets the GUID to all zeros. This function helps identify such scenarios during database migrations and troubleshooting.

Syntax

Find-DbaDatabase
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-Property] <String>]
    [-Pattern] <String>
    [-Exact]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Find-DbaDatabase -SqlInstance "DEV01", "DEV02", "UAT01", "UAT02", "PROD01", "PROD02" -Pattern Report

Returns all database from the SqlInstances that have a database with Report in the name

Example: 2
PS C:\> Find-DbaDatabase -SqlInstance "DEV01", "DEV02", "UAT01", "UAT02", "PROD01", "PROD02" -Pattern TestDB -Exact | Select-Object *

Returns all database from the SqlInstances that have a database named TestDB with a detailed output.

Example: 3
PS C:\> Find-DbaDatabase -SqlInstance "DEV01", "DEV02", "UAT01", "UAT02", "PROD01", "PROD02" -Property ServiceBrokerGuid -Pattern '-faeb-495a-9898-f25a782835f5' | Select-Object *

Returns all database from the SqlInstances that have the same Service Broker GUID with a detailed output

Required Parameters

-SqlInstance

The target SQL Server instance or instances.

PropertyValue
Alias
RequiredTrue
Pipelinetrue (ByValue)
Default Value
-Pattern

The search value to match against the specified property. Supports regular expressions for flexible pattern matching.
Use simple strings like ‘Sales’ or ‘Test’, or regex patterns like ‘^prod.*db$’ to match databases starting with ‘prod’ and ending with ‘db’.

PropertyValue
Alias
RequiredTrue
Pipelinefalse
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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default Value
-Property

Specifies which database property to search against: Name, Owner, or ServiceBrokerGuid. Defaults to Name for database name searches.
Use Owner when tracking down databases by their owner for security audits, or ServiceBrokerGuid when identifying databases with matching Service Broker configurations across environments.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueName
Accepted ValuesName,ServiceBrokerGuid,Owner
-Exact

Forces an exact string match instead of pattern matching. Use this when you need to find databases with names that exactly match your search term.
Particularly useful when searching for database names that contain regex special characters or when you want precise matches without wildcards.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse
-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.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse