Programmatically bulk add users Drupal 8 +

Recently I needed to add some new users to many different Drupal 8 / 9 developed sites. Adding more than just one user to each site through the administration ui is a bit time consuming IMO. If you can do something more efficiently that will be reused in the future, take a few minutes extra and do it. Then share it. The Drupal 7 version of this was found on CodeKarate, which has been an invaluable resource for so many Drupalers!

Enough talk, here is the code snippet:

<?php
/*
* adapted from d7 version via codekarate
* http://codekarate.com/blog/create-user-account-drupal-7-programmatically
*/
use Drupal\user\Entity\User;
$users = array(
'name1' => 'name1@email.com',
'name2' => 'name2@email.com',
'name3' => 'name3@email.com',
);
foreach ($users as $name => $email) {
//set up the user fields
$user = User::create([
'name' => $name,
'mail' => $email,
'pass' => user_password(),
'status' => 1,
'roles' => array('authenticated'),
]);
$user->save();
}

#drupal 8#drupal 9#drupal users