2

I am trying to run a Powershell command in my C# program but I get the error

the term is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I even copied this same exact command in Powershell manually and it worked.

The code follows as

PowerShell ps = PowerShell.Create();
var cmd = "scp -i \"path\\to\\ssh_key\" \"path\\to\\source_file\" [email protected]:/data/user_storage";
ps.AddCommand(cmd);
ps.Invoke();
0

2 Answers 2

3

The .AddCommand() PowerShell SDK method accepts the name or file path of a command, not an entire command line.

In order to execute a command line as you would in an interactive PowerShell session, use the .AddScript() method instead.

Alternatively, pass only the executable name/path to .AddCommand() and follow it with an .AddArgument() call for each argument.

Note that you don't strictly need PowerShell in order to execute an executable with arguments (unless you need shell features such as >).

Sign up to request clarification or add additional context in comments.

Comments

-2
   To run the "scp" command in PowerShell, you may need to specify the 
   full path to the "scp" executable file. You can find the path to the 
   "scp" executable file by running the following command in PowerShell:
Get-Command scp.exe

   Once you have the path to the "scp" executable file, you can modify 
      your C# code to use the full path to the executable, like this:
PowerShell ps = PowerShell.Create();
var cmd = "C:\\path\\to\\scp.exe -i \"path\\to\\ssh_key\" 
\"path\\to\\source_file\" [email protected]:/data/user_storage";
ps.AddCommand(cmd);
ps.Invoke();


Replace "C:\path\to\scp.exe" with the actual path to the "scp" 
 executable file on your system.

Alternatively, you can add the path to the "scp" executable file to the 
system PATH environment variable so that PowerShell can find it without 
specifying the full path. You can do this by following these steps:
  • Open the Start menu and search for "Environment Variables"

  • Click on "Edit the system environment variables"

  • Click on the "Environment Variables" button

  • Under "System Variables", scroll down and find the "Path" variable

  • Click on the "Edit" button

  • Click on the "New" button and add the path to the directory containing the "scp" executable file

  • Click "OK" to save the changes

    After adding the path to the system PATH environment variable, you should be able to run the "scp" command in PowerShell without specifying the full path.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.