liveBook · Manning

Dbatools

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

dbatools is a community-maintained PowerShell module that provides automation commands for SQL Server administration and migration.

dbatools is a community-maintained PowerShell module for automating SQL Server administration and migration. It provides a practical alternative when native tools do not cover every task, while avoiding custom SQL client code that may be harder to maintain. section 7.0 section 1.4.1

Use dbatools to:

  • Create SQL Server databases and database objects from PowerShell. section 7.0
  • Support operational workflows such as adding servers to a SQL database, searching asset records, and updating one or more server entries. section 7.0
  • Choose a maintained community solution when Microsoft modules lack required coverage, while comparing it against direct SQL access and other tools. section 1.4.1

Create a SQL Express database

Set the target instance and database name, package the parameters, and call New-DbaDatabase with Simple recovery for a non-mission-critical asset-management database. section 7.0

Listing 7.1 Creating PoshAssetMgmt database
$SqlInstance = "$($env:COMPUTERNAME)\SQLEXPRESS"
$DatabaseName = 'PoshAssetMgmt'
$DbaDatabase = @{
    SqlInstance   = $SqlInstance
    Name          = $DatabaseName
    RecoveryModel = 'Simple'
}
New-DbaDatabase @DbaDatabase

The listing shows the minimal parameterized pattern for creating a database with dbatools: define the SQL instance, name, and recovery model, then splat them into New-DbaDatabase. section 7.0

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Demonstrates a concrete database-creation command but stops short of teaching complex queries, backups, or preventive maintenance.
Examples
Uses a PoshAssetMgmt database, SQL Server Express, New-DbaDatabase, and server search and update operations.
What it assumes
Assumes PowerShell and SQL access, while avoiding any requirement for DBA expertise and using simple queries for interaction.
What you can do after
Apply it via the dbatools PowerShell module to create databases and automate basic SQL data workflows.
How it connects
Connects PowerShell automation with SQL Server, SQL databases, the PowerShell Gallery, and Microsoft’s SqlServer module.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.