How to tell win_powershell module which version of PowerShell to use

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

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

- 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

2 Likes

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.)

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