'PowerShell Start-Process -redirectStandardOutput throws: The system cannot find the file specified

In a PowerShell window I am executing:

Start-Process XXXXX.exe -ArgumentList "some valid arguments" -wait 
-redirectStandardOutput "D:\\test1.txt"

And getting:

Start-Process : This command cannot be executed due to the error: The system cannot find the file specified. At line:1 char:14 + Start-Process <<<< XXXXX.exe -ArgumentList "some valid arguments here" -redirectStandardOutput "D:\\test1.txt" + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

I have checked that the start-process works as expectedif I omit the -redirectStandardOutput argument (it does).

The test1.txt is a NEW file, not trying to append.

The suprising thing is that the test1.txt file on D:\ is created when I run the line, it just remains empty.

Has anyone any idea what is happening here? Thanks.

EDIT

I discovered that if I run:

Start-Process XXXXX.exe -ArgumentList "some valid arguments" -wait 
-redirectStandardOutput "D:\\test1.txt"

it fails (as originally posted) If I run:

Start-Process XXXXX.exe -ArgumentList "some valid arguments" -wait 

it works fine but doesnt save the console output to a file and if I run

Start-Process .\XXXXX.exe -ArgumentList "some valid arguments" -wait 
-redirectStandardOutput "D:\\test1.txt"

It works pretty much as expected. So why do I need to specify the path when I am using the redirection but when I am not it runs happily?

EDIT To recapitulate the problem; there appears to be some inconsistancy regarding the requirement that scripts/exes in the current directory require a ./ prefix to be allowed to run. When I am not redirecting the output the ./ is NOT required. Anyone know if this is expected behavior?



Solution 1:[1]

RichyRoo,

The issue you're seeing is most likely because your XXXXX.exe location isn't registered inside your PATH environment variable.

If you try to run your command using an application registered inside one of the folders defined inside your PATH environment variable, it should work without the trailing .\ ie:

Start-Process cmd.exe -ArgumentList "/c ping 127.0.0.1" -redirectStandardOutput "D:\ABC\test.txt" -Wait

I tried a 2nd example using a sample batch file and it doesn't work without prefixing the path by .\ - It replicates your behavior.

So it works on your end by qualifying your .exe location by prefixing your xxxxx.exe with .\ as I assume your current directory in your shell is your .exe location.

I haven't looked up why current path isn't being looked at the command execution level though.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 P-L