supermariowert supermariowert - 6 months ago 68
C Question

C system function returns sh: Invoke-Item command not found

I want to run a powershell command from c (gcc) and I was told to use the

system()
function and I get that output. The exact same thing happens in a vacuum as well (nothing else in the program). I am using windows. What I want to do is use
Invoke-Item
. What should I do?

Answer Source

To run PowerShell commands using system() function, you need to do something like this:

system("powershell -Command \"<your-command-here>\"");

AFAIK, the default shell on Windows is Command Prompt. PowerShell is another shell and you need to invoke it first to run commands in it.

Example (run dir command):

Using Command Prompt:

> PowerShell.exe -Command "dir"

Using Linux Terminal:

$ powershell -Command "dir"

So, the above command format should work for your case also i.e.:

system("PowerShell.exe -Command \"Invoke-Item ...\"");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download