Calling Plink from PowerShell

Plink is part of the PuTTY suite. Plink is primarily used to run automated operations (rather than interactive sessions) on a remote server, using the SSH protocol.

To reliably run Plink from PowerShell, you need to redirect Plink's input from $null. This avoids the problem that both PowerShell and Plink might attempt to read from the console, resulting in an exception: Cannot read keys when either application does not have a console or when console input has been redirected.

The function Invoke-Plink encapsulates the call of Plink:

function Invoke-Plink {

	$ErrorActionPreference = 'Continue';
	$null | & "$env:ProgramFiles\PuTTY\plink.exe" -batch -load $Session $Command;

Use the main PuTTY program to configure and save a session, then call this function as follows:

PS C:\> Invoke-Plink -Session '' -Command 'whoami'

Plink will send standard error output to PowerShell's Error stream. You can handle this output by