How to programmatically reset user password I'm trying to create a custom module for resetting the user's password. 

In the following block of code I'm attempting to get the password in hash form: 

$pass='User123@';

$autoloader =require_once(\Drupal::service('file_system')-&gt;realpath('autoload.php'));
//echo $autoloader; exit;
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod', FALSE);
$kernel-&gt;boot();
$password_hasher = $kernel-&gt;getContainer()-&gt;get('password');
$newhash =  $password_hasher-&gt;hash($pass);

//echo  $newhash ; exit; 


This is my db update code: 

 $updatepass = \Drupal::database()-&gt;update('users_field_data')
      -&gt;fields(array(
        'pass' =&gt; $newhash,
      ))
      -&gt;condition('name', "$id",'=')
      -&gt;execute();


Please help me understand what mistake I've made.
Just use the setPassword() method to update the user's password:

// Get user storage object.
$user_storage = \Drupal::entityManager()-&gt;getStorage('user');

// Load user by their user ID
$user = $user_storage-&gt;load($id);

// Set the new password
$user-&gt;setPassword('User123@');

// Save the user
$user-&gt;save();


This will take care of all the hashing for you and will trigger all the appropriate hooks for saving a user.