Running powershell script through the system command - output creates a file that is empty

Viewed 155

I am attempting to run a powershell script through the system command. This is the code I am using

 status = system("start powershell.exe Set-ExecutionPolicy RemoteSigned \n");
 status = system("start powershell.exe D:\\foo\\tempFilePShellCommand.ps1 > D:\\foo\\MyFile.txt");

Now the problem with the above code is that MyFile.txt gets created but its empty. If I run the file through powershell the file gets created and contains the correct data. Any suggestions on why the file is empty and how I can fix this /

1 Answers

You need to specify that the parameter is a file. Otherwise, it is treated as a parameter to "PowerShell.exe" instead of a file you want to run using PowerShell.

status = system("start powershell.exe -File D:\\foo\\tempFilePShellCommand.ps1 > D:\\foo\\MyFile.txt");
Related