How to create new customers using CLI in Magento 2

How to Create New Customers Using CLI in Magento 2

In this article, we will discuss how to create new customers using CLI in Magento 2.

Currently, you can’t create a new Customer like an Admin user using the CLI command in Magento 2. You can create a custom module in Magento to create your own CLI command for creating a new Customer.

Steps on How to Create New Customers Using CLI in Magento 2

1) Create a di.xml file in an etc folder in the default area:

<?xml version="1.0"?>
    <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\Framework\Console\CommandList">
                <argument name="commands" xsi:type="array">
                    <item name="customer_user_create" xsi:type="object">Vendor\Module\Console\Command\CustomerUserCreateCommand</item>


2) Create a file named CustomerUserCreateCommand.php at location app/code/Vendor/Module/Console/Command/CustomerUserCreateCommand.php

namespace Vendor\Module\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Vendor\Module\Helper\Customer;

class CustomerUserCreateCommand extends Command
    protected $customerHelper;

    public function __construct(Customer $customerHelper)
        $this->customerHelper = $customerHelper;

    protected function configure()
            ->setDescription('Create new customer')

    protected function execute(InputInterface $input, OutputInterface $output)
        $output->writeln('<info>Creating new user...</info>');

        $output->writeln('<info>User created with the following data:</info>');
        $output->writeln('<comment>Customer ID: ' . $this->customerHelper->getCustomerId());
        $output->writeln('<comment>Customer Website ID ' . $input->getOption(Customer::KEY_WEBSITE));
        $output->writeln('<comment>Customer First Name: ' . $input->getOption(Customer::KEY_FIRSTNAME));
        $output->writeln('<comment>Customer Last Name: ' . $input->getOption(Customer::KEY_LASTNAME));
        $output->writeln('<comment>Customer Email: ' . $input->getOption(Customer::KEY_EMAIL));
        $output->writeln('<comment>Customer Password: ' . $input->getOption(Customer::KEY_PASSWORD));

    protected function getOptionsList()
        return [
            new InputOption(Customer::KEY_FIRSTNAME, null, InputOption::VALUE_REQUIRED, '(Required) Customer first name'),
            new InputOption(Customer::KEY_LASTNAME, null, InputOption::VALUE_REQUIRED, '(Required) Customer last name'),
            new InputOption(Customer::KEY_EMAIL, null, InputOption::VALUE_REQUIRED, '(Required) Customer email'),
            new InputOption(Customer::KEY_PASSWORD, null, InputOption::VALUE_REQUIRED, '(Required) Customer password'),
            new InputOption(Customer::KEY_WEBSITE, null, InputOption::VALUE_REQUIRED, '(Required) Website ID'),
            new InputOption(Customer::KEY_SENDEMAIL, 0, InputOption::VALUE_OPTIONAL, '(1/0) Send email? (default 0)')


3) Now create a helper file named Customer.php as app/code/Vendor/Module/Helper/Customer.php

namespace Vendor\Module\Helper;

use \Magento\Framework\App\Helper\Context;
use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Framework\App\State;
use \Magento\Customer\Model\CustomerFactory;
use \Symfony\Component\Console\Input\Input;

class Customer extends \Magento\Framework\App\Helper\AbstractHelper
    const KEY_EMAIL = 'customer-email';
    const KEY_FIRSTNAME = 'customer-firstname';
    const KEY_LASTNAME = 'customer-lastname';
    const KEY_PASSWORD = 'customer-password';
    const KEY_WEBSITE = 'website';
    const KEY_SENDEMAIL = 'send-email';

    protected $storeManager;
    protected $state;
    protected $customerFactory;
    protected $data;
    protected $customerId;

    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        State $state,
        CustomerFactory $customerFactory
    ) {
        $this->storeManager = $storeManager;
        $this->state = $state;
        $this->customerFactory = $customerFactory;


    public function setData(Input $input)
        $this->data = $input;
        return $this;

    public function execute()

        $customer = $this->customerFactory->create();

        $this->customerId = $customer->getId();

        if($this->data->getOption(self::KEY_SENDEMAIL)) {

    public function getCustomerId()
        return (int)$this->customerId;

Now run the php bin/magento setup:upgrade CLI command in the terminal.

On running php bin/magento you will find customer:user:create CLI command listed in the existing command lists of Magento 2. Now use the below example to create a Customer in Magento and change the appropriate variables required.


php bin/magento customer:user:create –customer-firstname=”John” –customer-lastname=”Doe” –customer-email=”” –customer-password=”john123″ –website=”1″

After running the above CLI command you will find the Customer is created in your Magento 2 with the credentials provided by you in the above command.

You can also check more on creating a CLI commands in Magento 2 from Magento 2 official documentation

Please share if you find this article useful. Thanks and happy coding!

Also Read:

5 1 vote
Article Rating
Notify of
Inline Feedbacks
View all comments