# get windows-path and execute command

**URL:** <https://forum.ansible.com/t/get-windows-path-and-execute-command/38665>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [January 26, 2024, 3:20pm UTC](https://forum.ansible.com/t/get-windows-path-and-execute-command/38665 "2024-01-26T15:20:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![astrakid](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/astrakid/32/1789_2.png) [@astrakid](https://forum.ansible.com/u/astrakid)\
**Post date:** [January 26, 2024, 3:20pm UTC](https://forum.ansible.com/t/get-windows-path-and-execute-command/38665/1 "2024-01-26T15:20:33Z")

</div>

hi,  
i want to use “get-command” to get the location of a program. in the next step i want to execute this program with a full qualified location.  
this example is just an example, i know there are other modules for doing a ping.

i use  
(get-command ping).source  
to get the location. in the next step i want to execute this with a parameter:

- name: get pingpath  
win\_shell: “(get-command ping).source”)  
register: pingpath
- name test ping  
win\_shell: “{{ pingpath }} 9.9.9.9”

that throws an error because of an  
unexpected token: ":"

any idea how to fix this?

regards,  
andre

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 26, 2024, 4:52pm UTC](https://forum.ansible.com/t/get-windows-path-and-execute-command/38665/2 "2024-01-26T16:52:59Z")

</div>

I’d start by adding a debug step to show what you actually registered:

```auto
- name: Show what got registered in pingpath
  ansible.builtin.debug:
    msg: "{{ pingpath }}"

```

Given that using it that way produced unexpected results, perhaps it’s because the registered data aren’t what you expect.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [January 29, 2024, 3:10am UTC](https://forum.ansible.com/t/get-windows-path-and-execute-command/38665/3 "2024-01-29T03:10:53Z")

</div>

This will be because you are using the full result from win\_shell which includes things like changed, rc, stdout, stderr, etc. You need to do `- win\_shell: “{{ pingpath.stdout |trim }} 9.9.9.9” and say you want to use the stdout value of the result which contains the full path. The | trim is also needed to trim the newline the stdout result will have.

Putting that aside what is the purpose here, it’s inefficient because you are now using 2 tasks which involves setting up the connection and running a command when you can just achieve it normally in one task and rely on the normal PATH lookup. It also is more complicated because you might need to quote the value if the path contains a space in it.

- win\_command: ping.exe 9.9.9.9

---

<div class="post-metadata">

**Author:** ![astrakid](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/astrakid/32/1789_2.png) [@astrakid](https://forum.ansible.com/u/astrakid)\
**Post date:** [January 29, 2024, 6:59am UTC](https://forum.ansible.com/t/get-windows-path-and-execute-command/38665/4 "2024-01-29T06:59:03Z")

</div>

perfect, that did the trick.  
thx a lot!

regards,  
andre
