3

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?

RichyRoo
  • 31
  • 1
  • 1
  • 4
  • possible duplicate of [Redirection of standard and error output appending to the same log-file](http://stackoverflow.com/questions/8925323/redirection-of-standard-and-error-output-appending-to-the-same-log-file) – xXhRQ8sD2L7Z Mar 04 '15 at 01:25
  • not quite the same issue; that question is about appending to an existing file. My file is brand new. More details added in OQ. – RichyRoo Mar 04 '15 at 02:14
  • I have the same problem; Adding redirections just basically breaks `Start-Process`. Works fine without redirections (`-RedirectSTandardOutput` or `-RedirectStandardError`). Something fishy in how the redirection is implemented. – huoneusto May 24 '22 at 13:54

1 Answers1

0

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.

P-L
  • 523
  • 4
  • 14