hashicorp/vagrant · error · VagrantPlugins::CommandPS::Errors::ElevatedNoCommand

A command must be provided when the --elevated flag is provi

Error message

A command must be provided when the --elevated flag is provided for the powershell command. Please provide a command when using the --elevated flag and try again.

What it means

In `vagrant powershell`, the --elevated flag only applies to command execution mode. After option parsing, if options[:command] is nil while options[:elevated] is true, Vagrant raises Errors::ElevatedNoCommand: elevated interactive PowerShell sessions are not supported, so a --command is mandatory when elevating.

Source

Thrown at plugins/commands/powershell/command.rb:51

          o.on("-e", "--elevated", "Execute a powershell command with elevated permissions") do |c|
            options[:elevated] = true
          end
        end

        # Parse out the extra args to send to the ps session, which
        # is everything after the "--"
        split_index = @argv.index("--")
        if split_index
          options[:extra_args] = @argv.drop(split_index + 1)
          @argv                = @argv.take(split_index)
        end

        # Parse the options and return if we don't have any target.
        argv = parse_options(opts)
        return if !argv

        # Elevated option enabled means we can only execute commands
        raise Errors::ElevatedNoCommand if !options[:command] && options[:elevated]

        # Execute ps session if we can
        with_target_vms(argv, single_target: true) do |machine|
          if !machine.communicate.ready?
            raise Vagrant::Errors::VMNotCreatedError
          end

          if options[:command]
            if machine.config.vm.communicator != :winrm
              raise VagrantPlugins::CommunicatorWinRM::Errors::WinRMNotReady
            end

            out_code = machine.communicate.execute(options[:command].dup, elevated: options[:elevated]) do |type,data|
              machine.ui.detail(data) if type == :stdout
            end
            if out_code == 0
              machine.ui.success("Command: #{options[:command]} executed successfully with output code #{out_code}.")
            end

View on GitHub (pinned to 35f3160f4a)

Solutions

  1. Add --command: `vagrant powershell --elevated --command "choco install -y git"`.
  2. Drop --elevated if you want an interactive session (only from a Windows host with ps_client capability).
  3. Check `vagrant powershell -h` for the exact flag names on your Vagrant version.

Example fix

# before
vagrant powershell --elevated
# after
vagrant powershell --elevated --command "choco install -y git"
Defensive patterns

Strategy: validation

Validate before calling

if ARGV.include?('--elevated') && !ARGV.include?('--command')
  abort '--elevated requires --command "..."'
end
system('vagrant', 'powershell', *ARGV)

Try / catch

begin
  Vagrant::Environment.new.cli(['powershell', '--elevated', '--command', cmd])
rescue VagrantPlugins::CommandPowershell::Errors::ElevatedNoCommand => e
  warn e.message # attach a --command or drop --elevated
  exit 1
end

Prevention

When it happens

Trigger: `vagrant powershell --elevated` with no --command; passing the intended script after `--` (which only becomes extra_args for the session) instead of via --command; flag order is irrelevant, the combination is what fails.

Common situations: Expecting --elevated to open an elevated interactive shell on the guest; provisioning scripts that set the elevation flag but build the command string conditionally and end up empty.

Related errors


AI-assisted analysis of hashicorp/vagrant@35f3160f4a (2026-08-21). Data as JSON: /api/errors/e8af6b487b20a587. Report an issue: GitHub.