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

An error occurred while executing a PowerShell script. This

Error message

An error occurred while executing a PowerShell script. This error is shown below. Please read the error message and see if this is a configuration error with your system. If it is not, then please report a bug. Script: %{script} Error: %{stderr}

What it means

Before opening an interactive remote session, `vagrant powershell` runs scripts/enable_psremoting.ps1 on the host with the guest's -hostname/-port/-username/-password. If Vagrant::Util::PowerShell.execute returns a non-zero exit code, Errors::PowerShellError is raised with %{script} (the script path) and %{stderr} (PowerShell's error output). This is a host-side setup failure, not a guest session error.

Source

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

          ensure
            if result["PreviousTrustedHosts"]
              reset_ps_remoting_for(machine, ps_info)
            end
          end
        end
      end

      def ready_ps_remoting_for(machine, ps_info)
        machine.ui.output(I18n.t("vagrant_ps.detecting"))
        script_path = File.expand_path("../scripts/enable_psremoting.ps1", __FILE__)
        args = []
        args << "-hostname" << ps_info[:host]
        args << "-port" << ps_info[:port].to_s
        args << "-username" << ps_info[:username]
        args << "-password" << ps_info[:password]
        result = Vagrant::Util::PowerShell.execute(script_path, *args)
        if result.exit_code != 0
          raise Errors::PowerShellError,
            script: script_path,
            stderr: result.stderr
        end

        result_output = JSON.parse(result.stdout)
        raise Errors::PSRemotingUndetected if !result_output["Success"]
        result_output
      end

      def reset_ps_remoting_for(machine, ps_info)
        machine.ui.output(I18n.t("vagrant_ps.resetting"))
        script_path = File.expand_path("../scripts/reset_trustedhosts.ps1", __FILE__)
        args = []
        args << "-hostname" << ps_info[:host]
        result = Vagrant::Util::PowerShell.execute(script_path, *args)
        if result.exit_code != 0
          raise Errors::PowerShellError,
            script: script_path,

View on GitHub (pinned to 35f3160f4a)

Solutions

  1. Read %{stderr} in the message — it contains the actual PowerShell error.
  2. Verify guest reachability: ensure the Windows Remote Management firewall rule allows TCP 5985 (or 5986 for HTTPS) from the host.
  3. Confirm winrm username/password in the Vagrantfile match a guest admin account.
  4. Provision remoting in the guest first (Enable-PSRemoting -Force) via a shell provisioner, then retry.

Example fix

# before: hardened guest, no WinRM provisioning
# after: provision remoting before vagrant powershell
Vagrant.configure("2") do |c|
  c.vm.communicator = "winrm"
  c.vm.provision "shell", inline: "Enable-PSRemoting -Force; netsh advfirewall firewall add rule name=WinRM dir=in action=allow protocol=TCP localport=5985"
end
Defensive patterns

Strategy: try-catch

Validate before calling

require 'socket'
host, port = '192.168.121.50', 5985
begin
  TCPSocket.new(host, port).close
rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT
  abort "guest WinRM #{port} unreachable; fix firewall/listener before vagrant powershell"
end

Try / catch

begin
  env.cli(['powershell'])
rescue VagrantPlugins::CommandPowershell::Errors::PowerShellError => e
  stderr = e.extra_data[:stderr]
  warn "enable_psremoting failed: #{stderr}"
  # actionable: check guest firewall (5985), winrm creds, host TrustedHosts
  exit 1
end

Prevention

When it happens

Trigger: WinRM on the guest unreachable at the computed host/port (firewall, NAT address wrong); bad winrm username/password; host PowerShell execution policy blocking the script; TrustedHosts restrictions on the host preventing the connection attempt.

Common situations: Windows guest images without WinRM provisioned; vagrant-winrm/NAT setups where winrm_info resolves to an address the host cannot reach; passwords with characters that break argument quoting.

Related errors


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