1

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:[email protected]" "cd import" "option transfer binary" "put C:\_ZbankyNaOdoslanie\*.gpg" "put C:\_ZbankyNaOdoslanie\*.chk" "close" "exit"

Thank you.

1 Answer 1

3

You can invoke this line in PowerShell using the call operator & and assign it's stdout and/or stderr to a variable.

Example:

$output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2

The variable $output will be an array of strings. Each line of output will be an array element.

More examples:

  • Just stdout: $output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 2>$null
  • Just stderr: $output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 1>$null
  • Both: $output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 2>&1
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, Andy. I reinstalled WinSCP to c:\WinSCP to avoid white space in folder name, but it screams: The term 'C:\WinSCP\winscp.com /console /command ' is not recognized as the nam e of a cmdlet, function, script file, or operable program. Check the spelling o f the name, or if a path was included, verify that the path is correct and try again.
@culter Right, make sure you include a space between your closing quote and the WinSCP arguments like this: & "C:\Path To\winscp.com" /console /command "option batch abort" ...
Andy, there is no space now. I reinstalled it to C:\WinSCP directory.
@culter Ok... paste in the command your are trying to run please.
@culter That command worked fine for me from the PowerShell prompt: PS C:\> & "C:\Program Files\*\winscp.com" /console /command "option batch abort" "option confirm off" "open sftp://user:[email protected]" My winscp.com is still in program files.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.