2

I have following PowerShell script:

cmd /c script1.bat
cmd /c script2.bat

script1.bat at the end of execution have "pause" command, so the execution of my PS script stops.

How can send any key cmd.exe to avoid stopping script execution? NOTE: I can't change batch scripts - they are 3rd party.

1
  • why not convert it all to Powershell and leave the .bat files in the 1980s?
    – D3vtr0n
    Jan 16, 2013 at 20:15

4 Answers 4

6

You can pipe input to the program (cmd.exe) like this:

"X" | cmd /c script1.bat

0

You could put an empty file called pause.bat in the same directory. Then it would do nothing instead of pause.

0

The batch scripts may be 3rd party, but surely you can just a copy/backup and edit the content to remove the PAUSE command?

I sometimes put a PAUSE in if I am testing something and don't want the window to close, but otherwise I can't think of a good reason to keep that in.

-1

I had trouble getting the accepted answer to work for me due having an expression in the bat file path.

"x" | $Env:WRAPPER_HOME\bat\installService.bat $LOGFILE

Error is "Expressions are only allowed as the first element of a pipeline."

Here's what I got working (finally):

[PS script code]

& runner.bat bat_to_run.bat logfile.txt

[runner.bat]

@echo OFF

REM This script can be executed from within a powershell script so that the bat file
REM passed as %1 will not cause execution to halt if PAUSE is encountered.
REM If {logfile} is included, bat file output will be appended to logfile.
REM
REM Usage:
REM runner.bat [path of bat script to execute] {logfile}

if not [%2] == [] GOTO APPEND_OUTPUT
@echo | call %1
GOTO EXIT

:APPEND_OUTPUT
@echo | call %1  1> %2 2>&1

:EXIT
2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – SRack
    Feb 21, 2020 at 16:08
  • 1
    @SRack Thanks for pointing that out. I've editing my answer to include the solution here.
    – ScottD
    Feb 21, 2020 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.