3

First of all, I'm much more comfortable with Bash then I am with CMD \ Powershell.

How do I stop Powershell opening \ running commands in a new window, which closes as soon as it's finished, meaning I'm unable to see the output?

I'm currently running through some tutorials on Kubernetes, and the person is running kubectl and minikube, in powershell, and I'm unable to replicate \ troubleshoot anything as it opening the same commands in a new window, before closing them on completion or error. I've had these sorts of issues before running powershell and I've ended up using Linux VM's in order to get to bash instead.

EDIT - Running the Get-Command as requested returns

PS C:\Users\hardya>  ((Get-Command minikube).definition)
C:\PATH\minikube.exe
PS C:\Users\hardya>  ((Get-Command docker).definition)
C:\Program Files\Docker\Docker\Resources\bin\docker.exe
PS C:\Users\hardya>  ((Get-Command kubectl).definition)
C:\Program Files\Docker\Docker\Resources\bin\kubectl.exe
11
  • powershell just executes the command. It doesn't have anything to do with cmd.exe unless you or your command calls cmd.exe explicitly
    – phuclv
    Apr 29, 2019 at 14:16
  • 1
    Well, it looks like CMD. In that it's a typical black console window that get's opened. Not the usual blue of Powershell. Either way, running the command in another window instead of within the terminal I'm running it from. Apr 29, 2019 at 14:25
  • as I said, PowerShell never runs anything other that what the user specifies, regardless of inside the console or in another console. So what you ask is unanswerable. You need to provide more details, like what you're running and what your prompt is ((Get-Command prompt).definition)
    – phuclv
    Apr 29, 2019 at 14:32
  • 1
    (Get-Command prompt).definition is like echo $PS1 in Linux, not which (which is where.exe), so if it has suspect commands then those will be run every time you execute a command
    – phuclv
    Apr 29, 2019 at 15:07
  • 1
    These are not PowerShell commands, hence the reason they're started in a different console window. I don't clearly understand why some applications require their own window and others don't (e.g. running nslookup.exe on Windows doesn't open another window), else I'd post an answer explaining this. Apr 29, 2019 at 20:48

1 Answer 1

1

I was running cvs.exe using Start-Process and it spawned a new window every time, until I noticed the -NoNewWindow argument.

Try running your Kubernetes commands something like this:

$exe = Get-Command minikube
$arg = @('-foo', '-bar boo') # Whatever args you need, one arg per string
Start-Process -NoNewWindow -PassThru -FilePath $exe -ArgumentList $arg

If you need to wait for the command to finish before doing something else, do something like this:

$exe = Get-Command minikube
$arg = @('-foo', '-bar boo') # Whatever args you need, one arg per string
$proc = Start-Process -NoNewWindow -PassThru -FilePath $exe -ArgumentList $arg
$proc.WaitForExit()

Start-Process -Wait might also work, but I use the $proc.WaitForExit() because -Wait didn't work for me at some point, and I found the workaround at StackOverflow (naturally) and just keep using it because it works.

See also Start-Process -NoNewWindow: Start the new process in the current console window. By default on Windows, PowerShell opens a new window. On non-Windows systems, you never get a new window.

1
  • Thanks for the reply! Stumbled across this last night while trying to get a script running in task scheduler while logged out and needed both parts of your solution. Appreciate the link to the other answer as I didn't realize the -PassThru was required to get the object back.
    – Melikoth
    Jan 25 at 18:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .