Suppress PowerShell prompts when executing script from standard input

Let’s say that I have a script with multiple PowerShell commands I want to run on a remote computer, for example:

PS C:pathtoscript> Get-Content example.ps1
Write-Output "$Env:USERNAME"
Write-Output "$Env:COMPUTERNAME"

(In reality I am constructing the list of commands dynamically.) I can execute this script on the remote by running PowerShell and reading the script from standard input:

PS C:pathtoscript> Get-Content example.ps1 | ssh server 'powershell -File -'
PS C:Usersrcampion> Write-Output "$Env:USERNAME"
rcampion
PS C:Usersrcampion> Write-Output "$Env:COMPUTERNAME"
SERVER
PS C:Usersrcampion>
PS C:pathtoscript>

However, you can see that in addition to the output from the script, PowerShell is printing prompts and echoing the commands, which makes parsing the output more difficult. This behavior seems to be a special case of the -File option:

If the value of File is -, then commands are read from standard input. Running powershell -File - without redirected standard input starts a regular session. This is the same as not specifying the File parameter at all. When reading from standard input, the input statements are executed one statement at a time as though they were typed at the PowerShell command prompt.

about_PowerShell_exe

Things I have tried:

  • Using the -NonInteractive option has no effect.
  • Piping the input through another command (e.g. findstr ".*" | powershell -File -) has no effect (doesn’t count as ‘redirected’ standard input I guess?).
  • Redirecting standard input to itself (powershell -File <&0) results in an error (The handle could not be duplicated during redirection of handle 0.)
  • Using -Command "Invoke-Expression $input"; this actually gets a little closer, but doesn’t properly execute more than one command. With the above script I get the output:
    rcampion
    Write-Output
    SERVER
    PS C:pathtoscript>
    

    The only command executing is the first Write-Output, the second is being treated as an argument which is why it gets printed out too.

Is there any way to either tell PowerShell to not output the prompts or to trick it into thinking the input is redirected?