Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a console application that uses powershell instead of cmd

I have no idea if this is even a programming question, or something that is configured through my OS. But how would I get it so my console application uses Powershell instead of cmd? I know I can open Powershell and run the program from there, but even then, calls such as system("Get-Childitem") fail.

I'm using Visual C++ by the way, if that matters. But if it's a compiler specific thing, then I would also like to know how to do it with GCC.

like image 537
Benjamin Lindley Avatar asked Nov 22 '25 16:11

Benjamin Lindley


1 Answers

Look into the environment variable COMSPEC, which on Windows, and DOS 1.1 before that, controls which shell is utilized for launching programs that need a shell.

SET COMSPEC=%path_to_powershell%

You can do the same within your C++ program by calling the setenv C runtime call;

like image 112
Andy Finkenstadt Avatar answered Nov 24 '25 07:11

Andy Finkenstadt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!