I need to write the output of cmd command DIR to powershell variable(array). Is it possible? I need it because I use cmd scripting in WinSCP (SFTP client) and when I’m connected to sftp server, I can use cmd commands only, not powershell. But I need to get names and size of files that are in the remote directory to check if the transfer was successful. This is my script which connects to sftp and upload some files:
C:\"Program Files"\WinSCP\winscp.com /console /command "option batch abort" "option confirm off" "open sftp://login:password@195.22.89.55" "cd import" "option transfer binary" "put C:\_ZbankyNaOdoslanie\*.gpg" "put C:\_ZbankyNaOdoslanie\*.chk" "close" "exit"
Thank you.
You can invoke this line in PowerShell using the call operator
&and assign it’s stdout and/or stderr to a variable.Example:
The variable
$outputwill be an array of strings. Each line of output will be an array element.More examples:
$output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 2>$null$output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 1>$null$output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 2>&1