Executing external program with PowerShell

I need a PowerShell script to (among other things) execute commands (contained in script.txt) on a remote system.  The following works if I put it in a Windows command file (.cmd).  

C:\PuTTY\plink.exe -ssh -P 1234 -l username -pw xyz < %temp%\script.txt

How can I do this inline in PoweShell?  I expect I'll need to use the invoke-expression comdlet, but can't figure out how to handle the parameters and input redirection.
Who is Participating?
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
That's a pretty awful way to supply arguments. And it won't work, in particular the input redirection.
You should not need to do anything special here. The only change needed is to replace %temp% with $env:Temp
C:\PuTTY\plink.exe -ssh -P 1234 -l username -pw xyz < $env:Temp\script.txt

Open in new window

or, more Powershell'ish:
Get-Content $env:Temp\script.txt | C:\PuTTY\plink.exe -ssh -P 1234 -l username -pw xyz

Open in new window

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Try this:

$arg1 = ""
$arg2 = "-ssh"
$arg3 = "-P"
$arg4 = "1234"
$arg5 = "-l"
$arg8 = "xyz"
$arg9="< %temp%\script.txt"

$args = @($arg1,$arg2,$arg3,$arg4,$arg5,$arg6,$arg7,$arg8,$arg9)

 & "C:\PuTTY\plink.exe"  $args
McThumpAuthor Commented:
Works perfectly.  Thanks!!
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.