# win\_command not working ?

**URL:** <https://forum.ansible.com/t/win-command-not-working/24711>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [June 13, 2017, 12:01pm UTC](https://forum.ansible.com/t/win-command-not-working/24711 "2017-06-13T12:01:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wenceslas\_des\_Desert](https://avatars.discourse-cdn.com/v4/letter/w/e9bcb4/32.png) [@Wenceslas\_des\_Desert](https://forum.ansible.com/u/Wenceslas_des_Desert)\
**Post date:** [June 13, 2017, 12:01pm UTC](https://forum.ansible.com/t/win-command-not-working/24711/1 "2017-06-13T12:01:07Z")

</div>

t.Hi,

I’m currently trying to use the win\_command module of Ansible. Task (in a role, itself included in a playbook) looks like that :

`

- name: Test module  
win\_command: dir

`

That’s quite simple, really. Connection to the target works perfectly fine since I’ve been able to install features, and even run commands using the win\_shell module.  
But this particular task invariably fail with this error : (ansible -vvv)

`  
fatal: [myvmFQDN]: FAILED! =\> {  
“changed”: false,  
“cmd”: “dir”,  
“failed”: true,  
“msg”: “The system cannot find the file specified”,  
“rc”: 2  
}

`  
Using ansible -vvvvvv… does not help.

Target is a Windows 2012R2 server.

I really don’t know what’s going on here… I could certainly use win\_shell module instead, but I would rather understand.

Any clue ?

Thank you.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [June 16, 2017, 5:10am UTC](https://forum.ansible.com/t/win-command-not-working/24711/2 "2017-06-16T05:10:16Z")

</div>

Sure, the thing to understand is that win\_command is for running executables directly. This means two things, 1/ the user’s environment isn’t applied and 2/ you aren’t running inside a dos box or powershell window - you are just starting a .exe (and receiving back whatever it sends to stdout and stderr).

‘dir’ isn’t actually an executable - its a built-in feature of the cmd.exe program. So to run dir via win\_command, you’d have to run the cmd.exe program and pass it an argument to tell it do what ‘dir’ does, like this:

`ansible windowshosts -m win_command -a 'cmd.exe /c dir'`

Just to prove the point slightly, powershell also has ‘dir’ built-in to it. If you run

`ansible windowshosts -m win_command -a 'powershell.exe /c dir'`

You’ll get the dir output (but notice it is in a slightly different format from the cmd.exe version).

This may all seem odd as its unfamiliar to think of running windows commands without being inside either the cmd or powershell ‘shells’. But it is more secure to go straight to the .exe you want to use and run it without going via a shell, so it has its uses.

Jon
