I get the same error when i want to migrate my ADO pipeline from Online Agent (Linux) to onPrem agent (Self Hosted Agent) on Windows.
In order to fix my yaml pipeline i need to add pwsh: true on the task: PowerShell@2
task: PowerShell@2