Rajeshwar
Rajeshwar

Reputation: 11651

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

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 /

Upvotes: 1

Views: 675

Answers (1)

Guy S
Guy S

Reputation: 472

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");

Upvotes: 2

Related Questions