how can I access site configuration when in Drush command?

Global variable $config === null and when I use Config::get('key'), I get

Error: Using $this when not in object context in Drupal\Core\Config\Config::get() (line 85 of /var/www/drupalvm/drupal/web/core/lib/Drupal/Core/Config/Config.php)`

What is the recommended approach? I can't find it anywhere... thank you!


I actually intended to access Settings, not Config - that's where the confusion came from. Settings::get() works just fine. To get data from config, just follow the accepted answer.

4 Answers 4


Get is not a static method, so it should not be called statically (using ::). Use Drupal's service container and get the config service using \Drupal::service('config'), then get your config from that.


Drupal 9

Inject the config factory and the site settings and then you can get all config and settings you need.

# modules/custom/MY_MODULE/drush.services.yml
    class: \Drupal\MY_MODULE\Commands\MyModuleCommands
    arguments: ['@config.factory', '@settings']
      - { name: drush.command }

// modules/custom/MY_MODULE/src/Commands/MyModuleCommands.php

namespace Drupal\MY_MODULE\Commands;

use Drupal\Core\Site\Settings;
use Drush\Commands\DrushCommands;
use Drupal\Core\Config\ConfigFactoryInterface;

 * Provide Drush commands for MY MODULE.
class MyModuleCommands extends DrushCommands {

   * The config factory.
   * @var \Drupal\Core\Config\ConfigFactoryInterface
  protected $configFactory;

   * The site settings.
   * @var \Drupal\Core\Site\Settings
  protected $settings;

   * MyModuleCommands constructor.
  public function __construct(ConfigFactoryInterface $configFactory, Settings $settings) {
    $this->configFactory = $configFactory;
    $this->settings = $settings;

   * Get stuf
   * @command my:stuff
   * @aliases muff
   * @usage my:stuff
   *   Get stuff from site.settings
  public function getStuff() {

    $uuid = $this->configFactory->get('system.site')->get('uuid');

    $batch_size = $this->settings->get('entity_update_batch_size');

  • Does this use values overridden in settings.php? The built in drush cget command sadly does not.
    – liquidcms
    Commented Feb 10, 2023 at 16:59

You don't need to create a custom Drush command, this can be executed directly on the terminal. Example:

drush ev 'print_r(\Drupal\Core\Site\Settings::get("file_private_path"));'
  • Why did this answer get a down vote? I just ran this code and it works on 10.2.5. This is a useful technique to quickly get a setting.
    – Felix Eve
    Commented Jun 20 at 14:20
  • Although for readability I prefer to put a line break on the end: drush ev 'print_r(\Drupal\Core\Site\Settings::get("config_sync_directory")); echo "\n";'
    – Felix Eve
    Commented Jun 20 at 14:21

Try drush cget configName

Drush config-get Documentation


drush config-get system.site

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.