Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have powershell script execute batch command in same terminal

I have a powershell 2 script that I'm trying to develop. The purpose of this script is to wrap around a batch script and intelligently choose what version of said batch script to run. Somewhere along the lines in my code I have some logic that goes like this:

& $myCommand $args

$myCommand is the fully qualified filename of the batch file I want to run. $args is the args passed into this script. This works except it opens up a command window when running $myCommand. How do I prevent this so that the output is within the same powershell shell?

What's odd is that if I execute the command directly, it shows up the way I want it. So something like:

C:\myCommand.bat $args

Given that I need to choose which command I want to run at runtime, how do I make it so the output is in the same shell when I use the '&' to execute the command in the variable? Thanks!

like image 827
Jason Thompson Avatar asked Sep 15 '25 03:09

Jason Thompson


1 Answers

Use Start-Process with the -NoNewWindow parameter instead of &:

Start-Process -filepath C:\myCommand.bat -argumentList @("arg1","arg2") -NoNewWindow
like image 176
Paul Avatar answered Sep 17 '25 01:09

Paul