How to set a default theme programmatically
Looking at ThemeController::setDefaultTheme (the request handler for the /admin/appearance/default route), this should do the trick:

\Drupal::configFactory()
  -&gt;getEditable('system.theme')
  -&gt;set('default', 'machine_name')
  -&gt;save();