How to create Media entity and attach to user field
I've found the solution. 
I don't need to create a Media entity and append it to the User entity, since the profile picture field type is Image, so I just need to add the file-&gt;id() to the specific field. 
Here is the code:
$replaced = str_replace(&quot;\\&quot;, &quot;&quot;, $user[&quot;picture&quot;]);
$imageData = file_get_contents($replaced);
$fileName = basename($replaced);
$file = file_save_data(
      $imageData, 
      &quot;public://&quot;. $fileName, 
      FileSystemInterface::EXISTS_RENAME
);

$values = array(
      'name' =&gt; $user[&quot;name&quot;],
      'pass' =&gt; [
         'value' =&gt; $user[&quot;pass&quot;],
         'pre_hashed' =&gt; TRUE,
      ],
      'field_user_picture' =&gt; array('target_id' =&gt;$file-&gt;id()),
      'status' =&gt; $user[&quot;status&quot;],
      'mail' =&gt; $user[&quot;mail&quot;],
      'field_first_name' =&gt; $user[&quot;field_first_name&quot;],
      'field_last_name' =&gt; $user[&quot;field_last_name&quot;],
);

$userStorage = \Drupal::entityTypeManager()-&gt;getStorage('user');
$userEntity = $userStorage-&gt;create($values);

Then, if required, I can create the Media entity as following:
$media = Media::create([
          'bundle' =&gt; 'image',
          'uid' =&gt; $userEntity-&gt;id(),
          'langcode' =&gt; \Drupal::languageManager()-&gt;getDefaultLanguage()-&gt;getId(),
          'field_media_image' =&gt; [
             'target_id' =&gt; $file-&gt;id(),
             'alt' =&gt; $file-&gt;getFilename(),
          ],
          'thumbnail' =&gt; [
             'target_id' =&gt; $file-&gt;id(),
             'alt' =&gt; $file-&gt;getFilename(),
          ],
]);

$media-&gt;setName($fileName)-&gt;setPublished(TRUE)-&gt;save();