To run a command on a Windows host with Ansible, use the win_command module in a playbook task, point it at a host that is reachable over WinRM, and supply the command as a string. The module executes the command directly (no shell), so it is the right choice for running an executable such as ipconfig or a PowerShell script path. If you need shell features like pipes, redirection or environment variable expansion, use win_shell instead.

What You'll Need

  • An Ansible control node (Linux, macOS or WSL on Windows) with Ansible installed.
  • One or more Windows hosts with WinRM enabled and an account that can log in remotely. Microsoft documents the WinRM setup steps in its own docs; the ansible.windows collection's setup guide covers the same ground.
  • The ansible.windows collection installed on the control node: ansible-galaxy collection install ansible.windows.
  • An inventory entry for the Windows host, typically using ansible_connection: winrm and the appropriate WinRM transport settings.
  • Credentials with permission to run the target executable on the Windows machine.

Step-by-Step

  1. Confirm connectivity first. Run ansible -i inventory.ini winhost -m ansible.windows.win_ping. A successful ping means WinRM and authentication are working. If it fails, fix connectivity before writing any tasks.
  2. Create or open your playbook. A minimal playbook targeting the Windows group looks like this: a hosts: line pointing at your Windows group, gather_facts: false if you do not need facts, and a tasks: list.
  3. Add a task using ansible.windows.win_command. Give it a name and a win_command: value with the executable and arguments, for example win_command: ipconfig /all. Use the fully qualified collection name ansible.windows.win_command to avoid ambiguity.
  4. Quote paths with spaces. If the executable path contains spaces, wrap it in quotes inside the command string, e.g. win_command: '"C:\Program Files\App\tool.exe" --flag'. Backslashes in YAML strings need escaping or single quotes.
  5. Set the working directory if needed. The module accepts a chdir argument to change the directory before running the command. Use it when the executable expects to run from a specific folder.
  6. Control change reporting. By default win_command reports changed: true every run. Add changed_when: false for read-only commands, or changed_when based on the return code, so your playbook output stays meaningful.
  7. Capture output and return codes. Register the result with register: result, then inspect result.stdout, result.stderr and result.rc. Use failed_when if the command returns non-zero for a non-error reason.
  8. Run the playbook. Execute ansible-playbook -i inventory.ini playbook.yml and read the output. Check the task's rc and stdout in the verbose output if something looks wrong.
  9. Re-run with -v or -vvv when debugging. Higher verbosity shows the exact command sent and the raw WinRM response, which is the fastest way to spot quoting problems.

Common Mistakes

  • Using shell syntax in win_command. Pipes, &&, redirection and variable expansion do not work because no shell is involved. Switch to ansible.windows.win_shell when you need them.
  • Forgetting to quote paths with spaces. The command is split on whitespace, so an unquoted path breaks.
  • Assuming changed is meaningful. A read-only command still reports changed unless you set changed_when.
  • Running as the wrong user. WinRM logs in with the inventory credentials; if the command needs elevation, use become with the appropriate Windows configuration.
  • Mixing up win_command and win_shell. The former runs an executable directly; the latter runs through a shell.

Tips

  • Prefer win_command for deterministic executables and win_shell only when you truly need shell features.
  • Use creates: or removes: arguments to make a command idempotent when it produces a file.
  • Keep commands short and pass complex logic to a script file you copy with win_copy and run with win_command.
  • Store credentials in Ansible Vault rather than plaintext inventory files.
  • Test one host at a time with --limit before rolling out to a group.

Troubleshooting

If the task fails immediately, run win_ping first to confirm WinRM and authentication. If ping works but the command fails, check result.rc and result.stderr for the actual error. A common cause is quoting: re-check backslashes and spaces in the command string. If you see an error about the module not being found, install the ansible.windows collection and use the fully qualified name. If the command works interactively but not through Ansible, the difference is usually the user context or working directory; set chdir and confirm the account has rights to the executable. If WinRM itself is failing, verify the listener and firewall rules on the Windows host using Microsoft's WinRM documentation.

Frequently Asked Questions

What is the difference between win_command and win_shell?

win_command runs an executable directly without a shell, so shell operators do not work. win_shell runs the command through a shell, enabling pipes, redirection and variable expansion.

Does win_command run PowerShell by default?

No. It runs the executable you name. To run PowerShell, call powershell.exe explicitly with the appropriate arguments, or use win_shell.

Why does my task always report changed?

Because win_command cannot know whether the command changed anything. Set changed_when: false for read-only commands or base it on the return code.

Can I pass arguments with spaces?

Yes, but quote them within the command string. The module splits on whitespace, so unquoted arguments containing spaces are broken apart.

How do I run a command only if a file does not exist?

Use the creates: argument with the path of the file the command produces. The task is skipped when that file already exists.

Do I need the ansible.windows collection?

Yes for current Ansible versions. Install it with ansible-galaxy collection install ansible.windows and reference modules by their fully qualified names.