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.windowscollection's setup guide covers the same ground. - The
ansible.windowscollection installed on the control node:ansible-galaxy collection install ansible.windows. - An inventory entry for the Windows host, typically using
ansible_connection: winrmand the appropriate WinRM transport settings. - Credentials with permission to run the target executable on the Windows machine.
Step-by-Step
- 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. - 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: falseif you do not need facts, and atasks:list. - Add a task using
ansible.windows.win_command. Give it anameand awin_command:value with the executable and arguments, for examplewin_command: ipconfig /all. Use the fully qualified collection nameansible.windows.win_commandto avoid ambiguity. - 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. - Set the working directory if needed. The module accepts a
chdirargument to change the directory before running the command. Use it when the executable expects to run from a specific folder. - Control change reporting. By default
win_commandreportschanged: trueevery run. Addchanged_when: falsefor read-only commands, orchanged_whenbased on the return code, so your playbook output stays meaningful. - Capture output and return codes. Register the result with
register: result, then inspectresult.stdout,result.stderrandresult.rc. Usefailed_whenif the command returns non-zero for a non-error reason. - Run the playbook. Execute
ansible-playbook -i inventory.ini playbook.ymland read the output. Check the task'srcandstdoutin the verbose output if something looks wrong. - Re-run with
-vor-vvvwhen 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 toansible.windows.win_shellwhen you need them. - Forgetting to quote paths with spaces. The command is split on whitespace, so an unquoted path breaks.
- Assuming
changedis meaningful. A read-only command still reports changed unless you setchanged_when. - Running as the wrong user. WinRM logs in with the inventory credentials; if the command needs elevation, use
becomewith the appropriate Windows configuration. - Mixing up
win_commandandwin_shell. The former runs an executable directly; the latter runs through a shell.
Tips
- Prefer
win_commandfor deterministic executables andwin_shellonly when you truly need shell features. - Use
creates:orremoves: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_copyand run withwin_command. - Store credentials in Ansible Vault rather than plaintext inventory files.
- Test one host at a time with
--limitbefore 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.