Running PowerShell with C++

I am using C++ console application to make a batch file, then executing it by using:

 
ShellExecuteA(NULL, "open", "run.bat", NULL, NULL, SW_NORMAL);

The batch file then has:

1
2
PowerShell.exe -Command "&'%~dp0removePUPs.ps1'"
pause


However, if I were to run the .bat file without the C++ program executing it the PowerShell script works, but when I let the C++ console application run the .bat file the PowerShell script gives me this error:

"cannot be loaded because running scripts is disabled on this system."

I do run this as administrator and have ran Set-ExecutionPolicy RemoteSigned.
It is weird that this works when I just run the .bat file myself but doesnt when the C++ program runs the .bat file.

I am not sure if this is to do with C++ and the way I execute the bat file or if it is totally different and something with PowerShell.
Last edited on
PowerShell is an optional component of Windows and may not even be installed on some systems, is there a reason you need to use it? If you can write C++ you shouldn't need to use batch scripts of any kind - that's just limiting yourself to a more restrictive language for no reason.
I'd say that ShellExecuteA(NULL, "open", "run.bat", NULL, NULL, SW_NORMAL); does not run PowerShell.exe but cmd.exe
I am making a program which removes the default apps in Windows 10 like People, Mail, News etc... Because some of them cannot be removed by just clicking uninstall. My program also did other things in Windows 10, but as there is a powershell command to remove most of the Windows 10 apps I thought I could make a batch file which would run the script. I did this because I do not know what Get-AppxPackage *xboxapp* | Remove-AppxPackage does for example, yes I could do this in C++ but I am not sure what that code does completely as in what and where it deletes files etc...
If you can't get the PowerShell method to work, there is source code for C++ here that I think is what you want:
https://code.msdn.microsoft.com/windowsdesktop/Package-Manager-Deployment-ae818077
If you can't get the PowerShell method to work, there is source code for C++ here that I think is what you want:
https://code.msdn.microsoft.com/windowsdesktop/Package-Manager-Deployment-ae818077


Thanks! I was trying to find something like that, I will take a look at the source and see if that helps.
Topic archived. No new replies allowed.