Why does Start-Process
fail to find the executable (not in the path) if -RedirectStandardOutput
or -RedirectStandardError
are specified?
I.e.
[X:\] Start-Process -FilePath "prog.exe" -WorkingDirectory (Get-Location).Path
The program starts & executes as it should. But when I add output redirection, everything falls apart:
[X:\] Start-Process -FilePath "prog.exe" -WorkingDirectory (Get-Location).Path -RedirectStandardOutput stdout.txt
Start-Process: This command cannot be run due to the error: The system cannot find the file specified.
Redirecting with the 1>stdout.txt
operator works as expected.
This seems not to affect programs that reside in directories listed in PATH
. I cannot really figure out what's the logic here. Redirections should have nothing to do with resolving the binary path in the first place.
Running on Windows 10 Professional.
Update: Full trace & simple reproducer
PS> cat .\hello.c
#include <stdio.h>
int main(int argc, char** argv)
{
printf("Meh\n");
getchar();
return 0;
}
PS> cl hello.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30138 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
hello.c
Microsoft (R) Incremental Linker Version 14.29.30138.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:hello.exe
hello.obj
PS> Start-Process -FilePath hello.exe -WorkingDirectory (Get-Location).Path
PS> Start-Process -FilePath hello.exe -WorkingDirectory (Get-Location).Path -RedirectStandardOutput stdout.txt
Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
At line:1 char:1
+ Start-Process -FilePath hello.exe -WorkingDirectory (Get-Location).Pa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
Update 2:
Seems that using absolute path for the executable is a workaround for the issue. (Although it doesn't explain why output direction breaks the executable name/path resolution in the first place)