How do I programmatically create a list of new users? I'm trying to use the following drush script, but I keep getting errors. Currently, it is not properly setting the user role to Premium.

#!/user/bin/env drush
&lt;?php  
  $users = [ 
      "name" =&gt; "email",
      "name" =&gt; "email",
      "name" =&gt; "email",
      "name" =&gt; "email",
      "name" =&gt; "email",
      "name" =&gt; "email",
      "name" =&gt; "email",
      "name" =&gt; "email",
  ];
  foreach ($users as $key =&gt; $val) {
      drush_invoke("user-create", $val);
      drush_print("Created account for email " . $val);
      drush_set_option("password", $key);
      drush_invoke('user-password', $val); 
      drush_print("Created password for email " . $val . " with password " . $key);
      drush_set_option("mail", $val);
      drush_invoke('user-add-role', "Premium members");
      drush_print("Added premium role to " . $val);
  }
?&gt;


Is there a better way to achieve this?
If you are having trouble with drush_invoke, try drush_invoke_process, which has two advantages over drush_invoke:


It is executed in a separate process, so you can set options explicitly, and do not need to worry about option "pollution" from previous calls to drush_set_option()
The results of the operation, including all log messages, is returned as structured php data, so you have a better idea of what was done.


Example:

$values = drush_invoke_process("@self", "user-add-role", array("Premium members"), array('mail' =&gt; $val));


You will find the output of the command in $values['output'], and structured results (if available) in $values['object'].

Note that in general, there is nothing wrong with calling drush_invoke as you are doing above; you may use that technique if it works for your use case. It is likely to be faster, but may sometimes be fragile.