diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index 7b66911..7241775 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -778,15 +778,18 @@ function install_tasks($install_state) {
     // Load the profile install file, because it is not always loaded when
     // hook_install_tasks() is invoked (e.g. batch processing).
     $profile = $install_state['parameters']['profile'];
-    $profile_install_file = $install_state['profiles'][$profile]->getPath() . '/' . $profile . '.install';
-    if (file_exists($profile_install_file)) {
-      include_once \Drupal::root() . '/' . $profile_install_file;
-    }
-    $function = $install_state['parameters']['profile'] . '_install_tasks';
-    if (function_exists($function)) {
-      $result = $function($install_state);
-      if (is_array($result)) {
-        $tasks += $result;
+    // A CLI install (e.g. Drush) may have specified an invalid profile name.
+    if (!empty($install_state['profiles'][$profile])) {
+      $profile_install_file = $install_state['profiles'][$profile]->getPath() . '/' . $profile . '.install';
+      if (file_exists($profile_install_file)) {
+        include_once \Drupal::root() . '/' . $profile_install_file;
+      }
+      $function = $install_state['parameters']['profile'] . '_install_tasks';
+      if (function_exists($function)) {
+        $result = $function($install_state);
+        if (is_array($result)) {
+          $tasks += $result;
+        }
       }
     }
   }
@@ -806,7 +809,7 @@ function install_tasks($install_state) {
   // Allow the installation profile to modify the full list of tasks.
   if (!empty($install_state['parameters']['profile'])) {
     $profile = $install_state['parameters']['profile'];
-    if ($install_state['profiles'][$profile]->load()) {
+    if (!empty($install_state['profiles'][$profile]) && $install_state['profiles'][$profile]->load()) {
       $function = $install_state['parameters']['profile'] . '_install_tasks_alter';
       if (function_exists($function)) {
         $function($tasks, $install_state);
@@ -1450,8 +1453,16 @@ function _install_get_version_info($version) {
  */
 function install_load_profile(&$install_state) {
   $profile = $install_state['parameters']['profile'];
-  $install_state['profiles'][$profile]->load();
-  $install_state['profile_info'] = install_profile_info($profile, isset($install_state['parameters']['langcode']) ? $install_state['parameters']['langcode'] : 'en');
+  if (!empty($install_state['profiles'][$profile])) {
+    $install_state['profiles'][$profile]->load();
+    $install_state['profile_info'] = install_profile_info($profile, isset($install_state['parameters']['langcode'])
+      ? $install_state['parameters']['langcode'] : 'en');
+  }
+  else {
+    $install_state['profile_info'] = [
+      'dependencies' => [],
+    ];
+  }
 }
 
 /**
diff --git a/core/includes/install.inc b/core/includes/install.inc
index c9fd75d..c96b9f8 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -566,8 +566,15 @@ function drupal_verify_profile($install_state) {
   // after all the other dependencies have been installed.
   $present_modules[] = $profile;
 
-  // Verify that all of the profile's required modules are present.
-  $missing_modules = array_diff($info['dependencies'], $present_modules);
+  // Verify that all of the profile's required modules are present, including
+  // the profile itself: it can be missing if an incorrect name was specified
+  // during a CLI install (e.g. Drush).
+  if (!isset($info['type'])) {
+    $missing_modules = [$profile];
+  }
+  else {
+    $missing_modules = array_diff($info['dependencies'], $present_modules);
+  }
 
   $requirements = array();
 
diff --git a/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
index 3d68561..54655f2 100644
--- a/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
+++ b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
@@ -303,8 +303,11 @@ protected function filterByProfileDirectories(array $all_files) {
         // This extension doesn't belong to a profile, ignore it.
         return TRUE;
       }
-
       foreach ($this->profileDirectories as $weight => $profile_path) {
+        // $profile_path is empty if a nonexistent profile name is specified.
+        if (empty($profile_path)) {
+          continue;
+        }
         if (strpos($file->getPath(), $profile_path) === 0) {
           // Parent profile found.
           return TRUE;
