How to Create a Custom Console Command in Magento 2

Magento 2 out of the box supports a command-line interface (CLI). Using Magento 2 CLI you can perform multiple operations from installing Magento 2 software to installing Magento 2 module. There are many useful commands available to perform different tasks. However, sometimes you need a custom console command in Magento 2 that performs some complex data manipulation operation.

Today we will see how we can build a custom console command in Magento 2 module. We will highlight only the required files and not the whole module. We assume that the Magento 2 backend module is already created.

Create a Custom Console Command in Magento 2

Step 1: Create di.xml file under your module etc directory. Add below content in this file.

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
	<type name="MagentoFrameworkConsoleCommandList">
		<arguments>
			<argument name="commands" xsi:type="array">
				<item name="import" xsi:type="object">CodextblogDemoConsoleCommandImport</item>
			</argument>
		</arguments>
	</type>
</config>

Step 2: Create Import.php class file under your module Console/Command directory. Add below code in this file.

<?php
namespace CodextblogDemoConsoleCommand;

use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleOutputOutputInterface;

class Import extends Command
{

    const NAME_ARGUMENT = "csv_name";
    
    /**
     * {@inheritdoc}
     */
    protected function execute(
        InputInterface $input,
        OutputInterface $output
    ) {
        $name = $input->getArgument(self::NAME_ARGUMENT);
        $output->writeln("Importing CSV " . $name);
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setName("demo:import");
        $this->setDescription("Import data from csv file");
        $this->setDefinition([
            new InputArgument(self::NAME_ARGUMENT, InputArgument::OPTIONAL, "Csv Name")
        ]);
        parent::configure();
    }
}

Step 3: Command is ready. Run the below command in console.

php bin/magento demo:import test.csv

Once you run this console command, the execute method will run and print output Importing CSV test.csv. You can add your logic in the execute function. Please note that here we have defined one optional argument that is csv_name. As a result, we are passing the name of the csv file. However, If you do not require to pass the argument then you can remove the argument.

Written by

Chirag

Chirag is a Founder & eCommerce Technology Leader at MageArmy. He leads the eCommerce development team and drives the company’s technology vision, helping businesses build, scale, and optimize high-performing online stores. With years of experience in eCommerce development, he has worked with businesses across industries to solve complex technical challenges and create scalable digital commerce solutions. He loves sharing practical insights about eCommerce development, Magento, Adobe Commerce, and the latest trends in digital commerce to help online businesses grow and succeed.