Reset user password without using "Forgot password?" I know that in Drupal 7 I could reset the user #1 password via code.

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
require_once DRUPAL_ROOT . '/includes/password.inc';
$newhash = user_hash_password('newpass');
$updatepass = db_update('users') 
  -&gt;fields(array('pass' =&gt; $newhash))
  -&gt;condition('uid', '1', '=')
  -&gt;execute();


(user_hash_password() doesn't exist anymore in Drupal 8.)

Alternatively, I could use the following code.

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
require_once DRUPAL_ROOT . '/includes/password.inc';
$edit['pass'] = 'newpass';
$account= user_load(1);
user_save($account, $edit);


What is the equivalent code for Drupal 8? What API should I use for this purpose?
It's easier these days:

$account = \Drupal::entityTypeManager()-&gt;getStorage('user')-&gt;load(1);
$account-&gt;setPassword('new password');
$account-&gt;save();