2

I was wondering how to write code in c++ that could send commands to powershell. I'd like to be able to parse the output as well but at this point I'd just like to begin learning how to run commands and can learn to parse the output later. Thank you ahead of time to anyone able to help.

3

1 Answer 1

1

Here's a small sample that should get you going:

#include "stdafx.h"

using namespace System;
using namespace System::Collections::ObjectModel;
using namespace System::Management::Automation;

int _tmain(int argc, _TCHAR* argv[])
{
    PowerShell^ _ps = PowerShell::Create();
    _ps->AddScript("Get-ChildItem C:\\");
    auto results = _ps->Invoke();
    for (int i = 0; i < results->Count; i++)
    {
        String^ objectStr = results[i]->ToString();
        Console::WriteLine(objectStr);
    }

    return 0;
}

Make sure the /clr switch is enabled for your C++ project.

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

1 Comment

what is the significance of the ^ sign at the end of each class including PowerShell^ and String^?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.