Add an admin user to a Craft site

Feb 25, 2021

The Craft shell is only available if the yii2-shell package is installed:

composer require --dev yiisoft/yii2-shell

Start the shell:

./craft shell

Execute the command:

Craft::$app->elements->saveElement(new \craft\elements\User([
  'username' => '<username>',
  'email' => '<email>',
  'newPassword' => '<password>',
  'admin' => true,
  'passwordResetRequired' => false,
]));