0

The problem is about sending command to Powershell,

My C# application sends commands one by one in order to get right directory to execute some files but even I downloaded all the necessary packages application throws an error

An exception of type 'System.Management.Automation.CommandNotFoundException' in 'cd C:\\Users\\User1\\Documents\\pathToFileforExecute\\ ' is not recognized as the name of a cmdlet, function, script file, or operable program.

My NuGet Packages ; enter image description here

My code is like

ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", "Unrestricted");
                ps.Invoke();
                ps.AddCommand("cd C:\\Users\\User1\\Documents\\pathToFileforExecute\\");
                ps.AddCommand("./executeTheFile");
                ps.AddCommand("pwd");
                Collection<System.Management.Automation.PSObject> results = ps.Invoke();

Please help me to solve problem I found this feed but it didn't work for me https://github.com/PowerShell/PowerShell/issues/8119

I tried deleting the System.Management.Automation because I read this package shouldn't use by itself.

3
  • 1
    I rolled back your latest edit. It looks like you are trying to ask a new question. Please use the "Ask question" button, and do not edit your existing question. The given answers and comments only match your original question. Also the question might be useful to someone else, however by editing it away, you make it unaccessible for search engines. Commented Oct 9, 2023 at 7:15
  • @JSONDerulo Yes I am trying desperately to ask a question but the system is blocking. I am just a learner. Commented Oct 9, 2023 at 7:16
  • Please see the help center. If you are unable to find an answer why the system doesn't let you ask questions there, you can try to create a question on Stack Overflow Meta. Commented Oct 9, 2023 at 11:37

1 Answer 1

1

You are using AddCommand without the AddParameter option. The path is a parameter.

Try the following code:

ps.AddCommand("Set-Location").AddParameter("Path", C:\\Users\\User1\\Documents\\pathToFileforExecute\\);

Or you may use the AddScript method instead of AddCommand like in this post.

Note that the cd command is an alias for Set-Location in PowerShell.

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

6 Comments

Thanks for answer it worked ! I have been trying for 2 days to solve the problem. How can I execute the executable file with PS './' is not working like execute from PS shell ?
@SanberkKüçükkelepçe Try the Start-Process cmdlet and provide the path to the executable as a parameter.
BTW, why do you even need to use PowerShell from C#? You have C# at your hands. You could start the executable without using PowerShell (e.g., see stackoverflow.com/q/9679375/761095)
Is there way to use this format in PS './someFile.exe' ?
Okay I found it, "-NoNewWindow" . Thanks a lot
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.