@@ -20,13 +20,18 @@ function Get-DbaDatabase {
2020 For MFA support, please use Connect-DbaInstance.
2121
2222 . PARAMETER Database
23- Specifies one or more databases to include in the results. Supports wildcards and exact name matching.
23+ Specifies one or more databases to include in the results using exact name matching.
2424 Use this when you need to retrieve specific databases instead of all databases on the instance.
2525
2626 . PARAMETER ExcludeDatabase
27- Specifies one or more databases to exclude from the results. Supports wildcards and exact name matching.
27+ Specifies one or more databases to exclude from the results using exact name matching.
2828 Use this to filter out specific databases like test or staging environments from your inventory.
2929
30+ . PARAMETER Pattern
31+ Specifies a pattern for filtering databases using regular expressions.
32+ Use this when you need to match databases by pattern, such as "^dbatools_" or ".*_prod$".
33+ This parameter supports standard .NET regular expression syntax.
34+
3035 . PARAMETER ExcludeUser
3136 Returns only system databases (master, model, msdb, tempdb).
3237 Use this when you need to focus on system database maintenance tasks or validation.
@@ -164,6 +169,11 @@ function Get-DbaDatabase {
164169
165170 Returns databases 'OneDb' and 'OtherDB' from SQL Server instances SQL2 and SQL3 if databases by those names exist on those instances.
166171
172+ . EXAMPLE
173+ PS C:\> Get-DbaDatabase -SqlInstance SQL2,SQL3 -Pattern "^dbatools_"
174+
175+ Returns all databases that match the regex pattern "^dbatools_" (e.g., dbatools_example1, dbatools_example2) from SQL Server instances SQL2 and SQL3.
176+
167177 #>
168178 [CmdletBinding (DefaultParameterSetName = " Default" )]
169179 [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSUseSingularNouns" , " " , Justification = " Internal functions are ignored" )]
@@ -173,6 +183,7 @@ function Get-DbaDatabase {
173183 [PSCredential ]$SqlCredential ,
174184 [string []]$Database ,
175185 [string []]$ExcludeDatabase ,
186+ [string []]$Pattern ,
176187 [Alias (" SystemDbOnly" , " NoUserDb" , " ExcludeAllUserDb" )]
177188 [switch ]$ExcludeUser ,
178189 [Alias (" UserDbOnly" , " NoSystemDb" , " ExcludeAllSystemDb" )]
@@ -306,9 +317,21 @@ function Get-DbaDatabase {
306317 }
307318
308319 $backed_info = Invoke-QueryRawDatabases
320+
321+ # Helper function to test if a name matches any of the provided regex patterns
322+ $matchesPattern = {
323+ param ($name , $patterns )
324+ if (! $patterns ) { return $true }
325+ foreach ($pattern in $patterns ) {
326+ if ($name -match $pattern ) { return $true }
327+ }
328+ return $false
329+ }
330+
309331 $backed_info = $backed_info | Where-Object {
310332 ($_.name -in $Database -or ! $Database ) -and
311333 ($_.name -notin $ExcludeDatabase -or ! $ExcludeDatabase ) -and
334+ (& $matchesPattern $_.name $Pattern ) -and
312335 ($_.Owner -in $Owner -or ! $Owner ) -and
313336 ($_.state -ne 6 -or ! $OnlyAccessible )
314337 }
@@ -331,6 +354,7 @@ function Get-DbaDatabase {
331354 Where-Object {
332355 ($_.Name -in $Database -or ! $Database ) -and
333356 ($_.Name -notin $ExcludeDatabase -or ! $ExcludeDatabase ) -and
357+ (& $matchesPattern $_.Name $Pattern ) -and
334358 ($_.Owner -in $Owner -or ! $Owner ) -and
335359 ($_.RecoveryModel -in $RecoveryModel -or ! $_.RecoveryModel ) -and
336360 $_.EncryptionEnabled -in $Encrypt
@@ -340,6 +364,7 @@ function Get-DbaDatabase {
340364 Where-Object {
341365 ($_.Name -in $Database -or ! $Database ) -and
342366 ($_.Name -notin $ExcludeDatabase -or ! $ExcludeDatabase ) -and
367+ (& $matchesPattern $_.Name $Pattern ) -and
343368 ($_.Owner -in $Owner -or ! $Owner ) -and
344369 $_.ReadOnly -in $Readonly -and
345370 $_.IsAccessible -in $AccessibleFilter -and
0 commit comments