{"record":{"id":"4747900e35fb953e","repo":"hashicorp/vagrant","slug":"an-error-occurred-while-executing-a-powershell-scr","errorCode":null,"errorMessage":"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}","messagePattern":"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: %(.+?) Error: %(.+?)","errorType":"exception","errorClass":"VagrantPlugins::CommandPS::Errors::PowerShellError","httpStatus":null,"severity":"error","filePath":"plugins/commands/powershell/command.rb","lineNumber":108,"sourceCode":"          ensure\n            if result[\"PreviousTrustedHosts\"]\n              reset_ps_remoting_for(machine, ps_info)\n            end\n          end\n        end\n      end\n\n      def ready_ps_remoting_for(machine, ps_info)\n        machine.ui.output(I18n.t(\"vagrant_ps.detecting\"))\n        script_path = File.expand_path(\"../scripts/enable_psremoting.ps1\", __FILE__)\n        args = []\n        args << \"-hostname\" << ps_info[:host]\n        args << \"-port\" << ps_info[:port].to_s\n        args << \"-username\" << ps_info[:username]\n        args << \"-password\" << ps_info[:password]\n        result = Vagrant::Util::PowerShell.execute(script_path, *args)\n        if result.exit_code != 0\n          raise Errors::PowerShellError,\n            script: script_path,\n            stderr: result.stderr\n        end\n\n        result_output = JSON.parse(result.stdout)\n        raise Errors::PSRemotingUndetected if !result_output[\"Success\"]\n        result_output\n      end\n\n      def reset_ps_remoting_for(machine, ps_info)\n        machine.ui.output(I18n.t(\"vagrant_ps.resetting\"))\n        script_path = File.expand_path(\"../scripts/reset_trustedhosts.ps1\", __FILE__)\n        args = []\n        args << \"-hostname\" << ps_info[:host]\n        result = Vagrant::Util::PowerShell.execute(script_path, *args)\n        if result.exit_code != 0\n          raise Errors::PowerShellError,\n            script: script_path,","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/hashicorp/vagrant/blob/35f3160f4ad6edc3a9f3aa9570adfc1a4d73aaa5/plugins/commands/powershell/command.rb#L90-L126","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read %{stderr} in the message — it contains the actual PowerShell error.","Verify guest reachability: ensure the Windows Remote Management firewall rule allows TCP 5985 (or 5986 for HTTPS) from the host.","Confirm winrm username/password in the Vagrantfile match a guest admin account.","Provision remoting in the guest first (Enable-PSRemoting -Force) via a shell provisioner, then retry."],"exampleFix":"# before: hardened guest, no WinRM provisioning\n# after: provision remoting before vagrant powershell\nVagrant.configure(\"2\") do |c|\n  c.vm.communicator = \"winrm\"\n  c.vm.provision \"shell\", inline: \"Enable-PSRemoting -Force; netsh advfirewall firewall add rule name=WinRM dir=in action=allow protocol=TCP localport=5985\"\nend","handlingStrategy":"try-catch","validationCode":"require 'socket'\nhost, port = '192.168.121.50', 5985\nbegin\n  TCPSocket.new(host, port).close\nrescue Errno::ECONNREFUSED, Errno::ETIMEDOUT\n  abort \"guest WinRM #{port} unreachable; fix firewall/listener before vagrant powershell\"\nend","typeGuard":null,"tryCatchPattern":"begin\n  env.cli(['powershell'])\nrescue VagrantPlugins::CommandPowershell::Errors::PowerShellError => e\n  stderr = e.extra_data[:stderr]\n  warn \"enable_psremoting failed: #{stderr}\"\n  # actionable: check guest firewall (5985), winrm creds, host TrustedHosts\n  exit 1\nend","preventionTips":["Pre-provision guests with Enable-PSRemoting and a WinRM firewall rule.","Verify winrm username/password match a guest admin before opening sessions.","Run vagrant from an elevated Windows terminal when remoting setup touches TrustedHosts."],"tags":["vagrant","powershell","winrm","remoting","firewall"],"backgroundTag":"powershell-script-error","analyzedSha":"35f3160f4ad6edc3a9f3aa9570adfc1a4d73aaa5","analyzedAt":"2026-08-21T13:34:32.514Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}