That command you posted only writes to a file, ... | Out-File -FilePath D:\rcpw\rcpw.txt, and would not work regardless of PowerShell version. I assume you missed some of the contant of the D:\rcpw\rcpw.ps1 file?
... | Out-File -FilePath D:\rcpw\rcpw.txt
Looks like you took inspiration from this thread: