How to execute Powershell scripts over Cygwin /OpenSSH

I am connecting via SSH to a windows host running Cygwin SSHD (OpenSSH) server.
I would like to run powershell scripts on it remotely.

I did try several versions of SSH servers based on OpenSSH and none of them worked so far. (Cygwin SSH, CopSSH, RedHat Cygwin SSH).

The problem is that after connecting (via SSH) and executing powershell.exe, the console does not return to the command prompt  (does not accepty any input at all).
The only way to break the freeze is to do CTRL-C.

Powershell scripts that do not require any console input (e.g. do not ask (Y/N) question) run OK with this command (-n option):

ssh.exe -n user@mysshserver "powershell.exe C:\\\testdir\\\some_script.ps1"  (command typed in PS command prompt on the SSH client side)

Note: if a powershell script requires input (let's say the "some_script.ps1" has command "Read-Host" in it), it will freeze too.

When I make a full connection though (ssh.exe user@myserver) the console doe not come back to the command prompt by running any PS script. (CTRL-C is needed again).

After connecting  - ssh user@sshserver - executing of "powershell.exe" freezes the console (no input accepted) but running "powershell.exe /?" does return the console prompt correctly.

Q. how do I make powershell work over Cygwin SSH work?

Thank you.

P.S.   freeSSHd was the only free SSH server on which PS scripts worked (console was accepting input requests from PS scripts and console would return to the command prompt). I wonder if freeSSHd is in some way based on OpenSSH or it is an independent product. It has other limitations though so I would like to use Cygwin/OpenSSH here.

Who is Participating?
Hi, I don't know why powershell does not work properly with openssh but I solved my problem by piping the commands I want to run in powershell to the command "powershell -command -"
For example to list services on a windows server do:

echo "get-eventlog -logname security"  |  ssh 'the-windows-server' powershell -command -

Regards, Lars
drnavigatorAuthor Commented:
Thank you for your response.
I have already found the "powershell -command -" workaround since my last post.

If someone has a better solution for running powershell scripts/commands over OpenSSH please add to this post.

Additional note: SSH exec requests seem to execute fine if using the -n switch, e.g:

ssh -n user@server 'powershell c:\\\testdir\\\subdir\\\test.ps1'

Thank you,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.