Set UserGroups for existing User
You can assign a user to certain groups via

Craft::$app-&gt;getUsers()-&gt;assignUserToGroups($user-&gt;id, [1,2,3]);


Would assign them to the groups with the ids 1, 2 and 3