# How to tell win\_powershell module which version of PowerShell to use

**URL:** <https://forum.ansible.com/t/how-to-tell-win-powershell-module-which-version-of-powershell-to-use/3321>\
**Category:** Get Help\
**Tags:** powershell, windows, collections\
**Created:** [January 15, 2024, 3:04pm UTC](https://forum.ansible.com/t/how-to-tell-win-powershell-module-which-version-of-powershell-to-use/3321 "2024-01-15T15:04:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jszigetvari](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jszigetvari/32/1996_2.png) [@jszigetvari](https://forum.ansible.com/u/jszigetvari)\
**Post date:** [January 15, 2024, 3:04pm UTC](https://forum.ansible.com/t/how-to-tell-win-powershell-module-which-version-of-powershell-to-use/3321/1 "2024-01-15T15:04:19Z")

</div>

Dear Members,

I have an older version of Windows (2012 R2) that comes with an older version of PowerShell installed.  
I have also installed the latest (7.4.1) version of PowerShell on the machine, but apparentlu the ansible.windows.win\_powershell module will use the old PowerShell version when running psh code.  
Is there recommended way of telling the ansible.windows.win\_powershell module which version of PowerShell it should use on the target machine?

I have looked at the module documentation but I did not seem to find any module parameter that would influence which PowerShell version will get used on the target machine(s).

If there is no such module parameter, then what would be the recommended way of ensuring that the latest installed PowerShell version gets used on the target machines?

Thanks in advance!  
János

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [January 15, 2024, 4:46pm UTC](https://forum.ansible.com/t/how-to-tell-win-powershell-module-which-version-of-powershell-to-use/3321/2 "2024-01-15T16:46:12Z")

</div>

Hi, the docs have a good example. Specifying `executable: pwsh.exe` may help you.

> ```yaml
> - name: Run PowerShell script in PowerShell 7
> ansible.windows.win_powershell:
> script: |
> $PSVersionTable.PSVersion.Major
> executable: pwsh.exe
> arguments:
> - -ExecutionPolicy
> - ByPass
> register: pwsh_output
> failed_when:
> - pwsh_output.output[0] != 7
> 
> ```
> 
> [ansible.windows.win\_powershell module – Run PowerShell scripts — Ansible Documentation](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_powershell_module.html)

---

<div class="post-metadata">

**Author:** ![jszigetvari](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jszigetvari/32/1996_2.png) [@jszigetvari](https://forum.ansible.com/u/jszigetvari)\
**Post date:** [January 16, 2024, 11:32am UTC](https://forum.ansible.com/t/how-to-tell-win-powershell-module-which-version-of-powershell-to-use/3321/3 "2024-01-16T11:32:11Z")

</div>

Thanks for the answer.  
The good thing about this is that the PowerShell version that comes with Windows by default is called powershell.exe, and pwsh.exe is the one that you can separately install under C:\Program Files  
Plus the one that you can install separately will be in the PATH, so simply by specifying pwsh.exe you should end up running the most recent PowerShell version installed on the target machine. (I don’t know whether it’s possible to have PowerShell 6 and 7 installed on the same machine next to each other.)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 15, 2024, 11:32am UTC](https://forum.ansible.com/t/how-to-tell-win-powershell-module-which-version-of-powershell-to-use/3321/4 "2024-02-15T11:32:37Z")

</div>

Closed on February 15, 2024, 11:32am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
