2

I have the following code:

        PowerShell ps = PowerShell.Create();

        // Call the PowerShell.AddCommand(string) method, add 
        // the Get-Process cmdlet to the pipeline. Do 
        // not include spaces before or after the cmdlet name 
        // because that will cause the command to fail.
        ps.AddCommand("mkdir");
        ps.AddArgument("C:\\Users\\me\\Desktop\\test");

Unfortunately, when I run this piece of code, no directories are created. I guess I am misunderstanding how this works, but ideally, I want my final application to be able to run powershell commands and then grab the output directly within my application.

What am I doing wrong to execute a simple powershell command from my code?

1
  • 1
    add ps.Invoke() Commented May 26, 2017 at 18:29

1 Answer 1

3

You are missing ps.Invoke() command for your powershell command. Just add it after command is build. Here is MSDN article about how to execute powershell commands.

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

2 Comments

Perfect. Say I have a script that I have execute, ie (python test.py) and in the test file I print "hello world". How can I read the output from executing that script?
@ryan look at the article there is example which execute powershell and get back result printing it to console

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.