Automate your SQL server tasks using dbatools (Powershell)

What is dbatools?

Dbatools is an open-source module of PowerShell developed by big names from the SQL server community, MVPs, and other SQL server and PowerShell professionals. The dbatools uses SMO (server management objects) from the .Net framework, the same objects used bu SSMS (SQL Server management studio).

With SMO objects, we can manipulate any SQL Server object using the C# program language. In addition to SMO’s widespread use,  dbatools use the WMI/CIM (Windows Management Instrumentation/Common Information Model) to manipulate windows server objects.

With all this power, it is focused on the administrative tasks of SQL Server environments, implementing good practices for development, migration, and daily routines. Dbatools.io currently has more than 500 commands to assist you in your daily activities.

 

How to install dbatools?

There is only a requirement: PowerShell v3+. Once you meet these requirements, installation is simple:

  • Open PowerShell command window with Administrator rights (Right Click on Window and select run as an administrator);
  • Run the following command:
    • Install-Module dbatools;

      Figure 1 – Installing dbatools (Powershell command).
  • To give affirmative answers to the questions type Y (Yes) in the prompt.
  • That’s it! You are done.

You can also use the offline method to install in your server with you don’t have internet access.  First, download the package on https://dbatools.io/zip and copy it preferably to some path that is configured in your environment variable $ env: PSModulePath.

  • Open the Start Search, type in “env”, and choose “Edit the system environment variables”.
  • Click the “Environment Variables…” button

    Figure 2 – Environment Variables.
  • After copying the package to one of the above locations, you can install Dbatools using the command:
    • Import-Module -Name Dbatools

Are you looking for more in-depth information? Read Installing modules from the PowerShell Gallery and Offline installs of dbatools.

 

How to check if dbatools was correctly installed?

One way is to use the Get-Module command presented below.

  • Get-Module dbatools -ListAvailable

    Figure 3 – Checking if dbatools is installed.

If you don’t receive that message, it may mean that the module is not installed on the machine or has not been loaded into the memory yet.

 

Getting started with dbatools

Let’s navigate through Powershell using the native cmdlets. Below are some commands that you will use in your everyday life.

  • Get-Command – List all package commands installed in the machine.
  • Get-Help – Get command details.
  • Get-command -Module dbatools – List all commands from dbatools module.
  • Get-command *Get-DbaSQL* – To filter the search, use the asterisk (*) wildcard character with the Name parameter.
  • Get-Help Get-DbaSqlService -Examples – The Examples parameter displays the help file’s NAME and SYNOPSIS sections and all the Examples, as you can see below:
  • Get-Help Get-DbaSqlService -Detailed – The Detailed parameter displays the help article’s detailed view that includes parameter descriptions and example, as you can see below:

The better your knowledge of Powershell, the easier it will be to work with dbatools, and the easier it will be to extract rich information.

More information

The dbatools commands automate and simplify many of the tasks we’ve all had to do multiple times. Want to know more? Here are some great articles that focus on functionality:

Conclusion

PowerShell is everywhere.

Learning PowerShell prepares you for the inevitable (and fun!) automation-rich future.

Once you learn how to use an automation tool like PowerShell, you can easily transition your skills to automate everything from on-premise Instances (Windows/Linux) to the Azure SQL database (it even helps power the cloud).

Want to do a custom SQL project? Want occasional help? Design database? Troubleshoot past problems? Something else? Contact us!

Share this post

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

4.8/5

42

TESTIMONIALS

FREE Scripts to Instantly
Improve SQL Server Performance

3 FREE Scripts to Instantly
Improve SQL Server Performance

Learn how to increase Performance of SQL Server INSTANTLY with 3 free scripts.