Ansible, PowerShell and Output from Cmdlets with ProgressPreference

I’ve posted previously regarding returning output from PowerShell scripts that have been invoked by Ansible playbooks:

I encountered an issue recently where noise from a particular PowerCLI cmdlet, Invoke-VMScript, was polluting the output returned from the PowerShell script to Ansible. Specifically it looked like the below, a mess of Invoke-VMScript across the screen:

This is because Invoke-VMScript is one of those cmdlets which displays progress to the console while it is executing:

Since I was not interested in any progress info polluting the text output I was returning from PowerShell, I set the Preference Variable:

$ProgressPreference = ‘SilentlyContinue’

i.e. to hide any progress output in the script. The noise was then absent from the text output I was returning.