diff --git a/commands/core/site_install.drush.inc b/commands/core/site_install.drush.inc
index 158f8fa..392b44c 100644
--- a/commands/core/site_install.drush.inc
+++ b/commands/core/site_install.drush.inc
@@ -94,16 +94,6 @@ function drush_core_pre_site_install($profile = NULL) {
     }
   }
 
-  // If there is a profile, write it (as needed) into $conf['install_profile']
-  // to help drush find commandfiles earlier in the drush bootstrap.
-  if (!empty($profile)) {
-    $contents = file_get_contents($settingsfile);
-    if (strpos($contents, "\$conf['install_profile']") === FALSE && is_writable($settingsfile)) {
-      $install_profile_config = "\$conf['install_profile'] = '$profile';";
-      $appended = file_put_contents($settingsfile, "\n" . $install_profile_config . "\n", FILE_APPEND);
-    }
-  }
-
   // The Drupal 6 installer needs to bootstrap up to the specified site.
   // We need to be at least at DRUSH_BOOTSTRAP_DRUPAL_SITE to select the site uri to install to
   define('MAINTENANCE_MODE', 'install');
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index bf7991a..96723b8 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -949,6 +949,21 @@ function _drush_bootstrap_drupal_full() {
     module_implements('watchdog', FALSE, TRUE);
   }
 
+  // Write correct install_profile to cache as needed. Used by _drush_find_commandfiles().
+  $cid = drush_cid_install_profile();
+  if ($cached_install_profile = drush_cache_get($cid)) {
+    // We have a cached profile. Check it for correctness and save new value if needed.
+    $install_profile = drush_drupal_major_version() >= 7 ? drupal_get_profile() : variable_get('install_profile', 'standard');
+    if ($cached_install_profile->data != $install_profile) {
+      drush_cache_set($cid, $install_profile);
+    }
+  }
+  else {
+    // No cached entry so write to cache.
+    $install_profile = drush_drupal_major_version >= 7 ? drupal_get_profile() : variable_get('install_profile', 'standard');
+    drush_cache_set($cid, $install_profile);
+  }
+
   _drush_log_drupal_messages();
 }
 
diff --git a/includes/command.inc b/includes/command.inc
index fefa407..8ab2d36 100644
--- a/includes/command.inc
+++ b/includes/command.inc
@@ -1325,7 +1325,7 @@ function _drush_find_commandfiles($phase, $phase_max = FALSE) {
       // or not.  If we are, however, going to continue on to bootstrap
       // all the way to DRUSH_BOOTSTRAP_DRUPAL_FULL, then we will
       // instead wait for that phase, which will more carefully add
-      // only those Drush command files that are associated with
+      // only those Drush commandfiles that are associated with
       // enabled modules.
       if ($phase_max < DRUSH_BOOTSTRAP_DRUPAL_FULL) {
         $searchpath[] = conf_path() . '/modules';
@@ -1336,13 +1336,24 @@ function _drush_find_commandfiles($phase, $phase_max = FALSE) {
           $searchpath[] = 'modules';
         }
       }
-      if ($phase_max < DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION) {
-        // Too early for variable_get('install_profile', 'default'); look in all profiles.
-        // If we're definitely going to get to DRUPAL_CONFIGURATION, then wait and
-        // see if we have 'install_profile' available there.
+
+      $cid = drush_cid_install_profile();
+      if ($cached = drush_cache_get($cid)) {
+        $profile = $cached->data;
+        $path = "profiles/$profile/modules";
+        if (file_exists($path)) {
+          $searchpath[] = $path;
+        }
+        elseif (file_exists('sites/all/' . $path)) {
+          $searchpath[] = 'sites/all/' . $path;
+        }
+      }
+      else {
+        // If install_profile is not available, scan all profiles.
         $searchpath[] = "profiles";
         $searchpath[] = "sites/all/profiles";
       }
+      
       // TODO: Treat themes like modules and stop unconditionally searching here.
       $searchpath[] = 'sites/all/themes';
       $searchpath[] = conf_path() . '/themes';
@@ -1352,25 +1363,7 @@ function _drush_find_commandfiles($phase, $phase_max = FALSE) {
       }
       break;
     case DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION:
-      // See comment above regarding this if() condition.
-      if ($phase_max < DRUSH_BOOTSTRAP_DRUPAL_FULL) {
-        // You must define your install_profile in settings.php. The DB is not sufficient.
-        // Drupal core does not yet do that. See http://drupal.org/node/545452.
-        if ($profile = variable_get('install_profile', NULL)) {
-          $path = "profiles/$profile/modules";
-          if (file_exists($path)) {
-            $searchpath[] = $path;
-          }
-          elseif (file_exists('sites/all/' . $path)) {
-            $searchpath[] = 'sites/all/' . $path;
-          }
-        }
-        else {
-          // If 'install_profile' is not available, check all profiles.
-          $searchpath[] = "profiles";
-          $searchpath[] = "sites/all/profiles";
-        }
-      }
+      // Nothing to do here anymore. Left for documentation.
       break;
     case DRUSH_BOOTSTRAP_DRUPAL_FULL:
       // Add enabled module paths. Since we are bootstrapped,
diff --git a/includes/drupal.inc b/includes/drupal.inc
index 7e5e11a..b5b7c9f 100644
--- a/includes/drupal.inc
+++ b/includes/drupal.inc
@@ -247,3 +247,11 @@ function _drush_drupal_parse_info_file($data, $merge_item = NULL) {
   return FALSE;
 }
 
+/*
+ * Build a cid to store the install_profile for a given site.
+ */
+function drush_cid_install_profile() {
+  // Todo: this CID needs to be site-specific and ideally would change if a site got reinstalled.
+  return drush_get_cid('install_profile', array(), array());
+}
+
