So it sounds very likely that the difference is being caused by ansible running the powershell remotely, rather than locally on the windows machine (where the script runs fine).
You can try adding some lines like the following in to your powershell script to see which line it is failing on.
Echo "my message" | Out-File -FilePath c:\temp\output.txt
Once you have worked out which line is causing it to fail, then I suggest running Get-Help on the command that is failing in powershell.
For example, if it is failing on a line that starts
Invoke-Item
then run
Get-Help Invoke-Item in a powershell windows on your remote windows machine.
Invoke-Item is one of the commands which may need a -Credential object passed to it if, for example, you are using the command to connect to a windows file share.
Some background information here on running remote powershell commands:
and information on Invoke-Item here:
Hope this helps.
Please let us know what you find.
Jon