C++ and Powershell
10,540
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.
Author by
sdd1208
Updated on June 04, 2022Comments
-
sdd1208 almost 2 years
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.
-
ShresthaGanesh almost 3 yearswhat is the significance of the ^ sign at the end of each class including PowerShell^ and String^?