Try using “^|” instead of “|” and show the results.
powershell.exe command fails when there is a pipe in the command using system for execution

SilverChips
71
Reputation points
I am trying to run a powershell command like this
std::string str = "powershell.exe -executionPolicy bypass Select-String -Path C:\\myfile.txt -Pattern \"output:\" | Select -ExpandProperty > c:\\result.txt";
system(str.c_str());
This command fails and the reason it fails is because there is a pipe so result.txt
is always empty.
Any idea why this happens and how I can fix this ?
Windows for business | Windows Server | User experience | PowerShell
8,330 questions
2 answers
Sort by: Most helpful
-
-
Anonymous
2021-02-08T03:53:44.817+00:00 Hi,
Please check if this works
powershell.exe -executionPolicy bypass -command "& {Select-String -Path C:\\myfile.txt -Pattern \"output:\" | Select -ExpandProperty Line > c:\\result.txt}"
Best Regards,
Ian Xue============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.