Customizing a user object

Loading the object

<?php
// Load the currently logged in user
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());

// Or load any user:
use Drupal\user\Entity\User;
$user = User::load($uid);

// Procedurally:
$user = user_load($uid);

Customizing

<?php
// The User class inherits the magic setter method __set() from
// parent class ContentEntityBase, enabling you to do:
$user->name = $name;
$user->mail= $mail;
$user->save();

// This works too: 
$user->setUsername($name); 
$user->setEmail ($mail); 
$user->save();

// Or this: 
$user->set('name', $name);
$user->save();

Setting a custom field

$user->set('field_occupation', 'Web developer');

Removing a role

<?php
$user->removeRole($rid);
// Role identifier is a string now, e.g. $user->removeRole('editor');

Setting the password

<?php
$user->setPassword('unhashed-password');

Activating the account

<?php
$user->activate();

More methods are found here.