Score:0

PowerShell on Windows: Start-Process fails if output redirection arguments are used

rs flag

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)

JosefZ avatar
th flag
Weird… What's output from `where.exe "prog.exe"`? Before and after the _running_ example (i.e. without any `-RedirectStandard` parameter)? Maybe `prog.exe` changes `$env:PATH` or a drive mapping?
rs flag
The output of the program doesn't really matter/have any effect, as the breakage happens in PowerShell, before the program is even executed. The programs I'm trying to execute using this scheme are all Windows console apps without GUI. Updated the question with a minimal reproducer.
rs flag
Forgot to add that the program to be run is our internal application, so I know it is not tinkering with `PATH` -- unless the MSVC CRT implementation does something fishy when output is piped :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.