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 {
	[CmdletBinding()]
	param(
		[string]
		$Session,
			
		[string]
		$Command
	);

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

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

PS C:\> Invoke-Plink -Session 'example.net' -Command 'whoami'
root

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