How To Programmatically Change Customer Password In Magento 2

Written by Jigar Patel

Dec 19, 2021

How To Programmatically Change Customer Password  In Magento 2

Password is a unique key that customers must use to access their accounts in your store, Changing password in Magento is not a big deal if you are logged in to the My Account area/Admin dashboard. I am talking about changing a customer’s password using a program. The below code will show you how to programmatically change a customer password without validating an old password.

Magento 2 new customer account is created via sign up, Also customer registration is possible via the admin panel. But, you can easily reset the password in your account.

Read This: How to Add Customer Attribute Programmatically in Magento 2

Programmatically change customer password create a PHP file in Magento root directory using following code. Do not forget to specify the customer id and password you want to set inside the code.

Step To Programmatically Change Customer Password in Magento 2:

use Magento\Framework\AppInterface;
ini_set('display_errors', 1);
try {
    require '../app/bootstrap.php';
} catch (\Exception $e) {
    echo $e->getMessage();
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('\Magento\Framework\App\State');

    $customerRepositoryInterface = $objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface');
    $customerRegistry = $objectManager->get('\Magento\Customer\Model\CustomerRegistry');
    $encryptor = $objectManager->get('\Magento\Framework\Encryption\EncryptorInterface');

    $customerId = 11; // here assign your customer id
    $password = "Dolphin123"; // set your custom password
    $customer = $customerRepositoryInterface->getById($customerId);
    $customerSecure = $customerRegistry->retrieveSecureData($customerId);
    $customerSecure->setPasswordHash($encryptor->getHash($password, true));
    echo 'Successfully Changes Your Password.';
catch(\Exception $e){

customerRepositoryInterface is an instance of \Magento\Customer\Api\CustomerRepositoryInterface
customerRegistry is an instance of \Magento\Customer\Model\CustomerRegistry
encryptor is an instance of \Magento\Framework\Encryption\EncryptorInterface

Successfully executing this script your customer will be able to log in to their account using “Dolphin123” password.

This way we can update customer password in Magento2.

hope this instruction will be helpful for you.

Jigar Patel


We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.











    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP