In Drupal 9, the custom Drush command file should have a .drush.yml extension instead of .drush.inc.
- Create a custom module: Create a custom module named “my_custom_module” by following Drupal’s module development guidelines.
- Create the
drush.services.ymlfile: Inside themy_custom_modulemodule directory, create adrush.services.ymlfile. - Define the command service: Open the
drush.services.ymlfile and define your custom Drush command as a service. Here’s the content of thedrush.services.ymlfile:
services:
utilities.commands:
class: \Drupal\utilities\Commands\CustomUtilities
tags:
- { name: drush.command }
- Create the command class: Create a new PHP class named
CustomUtilitiesin theutilities/src/Commandsdirectory. The class should extend theDrushCommandsclass provided by Drush. Here’s an example of theCustomUtilitiesclass:
<?php
namespace Drupal\utilities\Commands;
use Drush\Commands\DrushCommands;
use Drupal\Core\Entity\EntityTypeManagerInterface;
/**
* A Drush commandfile.
*
* In addition to this file, you need a drush.services.yml
* in root of your module, and a composer.json file that provides the name
* of the services file to use.
*/
class CustomUtilities extends DrushCommands {
/**
* Echos back hello with the argument provided.
*
* @command bot
* @param string $arg1
* The first argument.
* @param string $arg2
* The second argument.
* @option string $option1
* The first option.
* @option string $option2
* The second option.
* @aliases d9-bot
* @usage bot [arg1] [arg2] --option1=value --option2=value
* Execute my custom command.
*/
public function bot($arg1, $arg2, $options = ['option1' => NULL, 'option2' => NULL]) {
$this->output()->writeln('Hello ' . $arg1 . ','. $arg2 . '! Passed options are:'. $options['option1']. ' & '. $options['option2'] );
}
}
In the example above, the CustomUtilities class fetches the user input argument and displays it as console output.
Refer to the below screens for folder structure,

- Clear the Drush cache: After creating the command file and the command class, clear Drush’s cache by running the following command in your Drupal root directory:
drush cache:clear drush
- Test the command: Finally, you can test your custom Drush command by running it in the command line:
drush bot Sankar Prakash --option1=Drupal --option2=React
See the below output from the custom drush command function.




