microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
95.28k stars 8.27k forks source link

Using Powershell to open CMD script and after that closing with the Powershell script, leaves a phantom window. #16516

Closed NekoJonez closed 9 months ago

NekoJonez commented 9 months ago

Windows Terminal version

1.18.3181.0

Windows build number

10.0.22621.0

Other Software

VSCode Microsoft Office 365 TeamViewer Synlogy Drive

I run the latest stable version of VSCode and I run PowerShell 7.4. (I have also written my script in PowerShell 7)

Steps to reproduce

I have written a Powershell script that calls upon a "CMD" file that checks if Microsoft Access is opened or not. If MS Access is opened, the CMD script will close it. That way my Powershell script can copy all the right files and move them over to our Synlogy Drive folder to back them up.

Now, I open the CMD file with this line in Powershell:

$CmdProcess = Start-Process -FilePath "cmd.exe" -ArgumentList "/c `"C:\Users\****\CloseAccess.bat`"" -PassThru

At the end of my Powershell script I have:

    Stop-Process -Id $CmdProcess.Id -Force
    Stop-Process -Name WindowsTerminal -Force -ErrorAction SilentlyContinue  

Expected Behavior

You would think that the script is closed and no other window is left behind.

Actual Behavior

Yet, this isn't the case. You are left with this: image

When you click on the "X" button, nothing happens. You really need to open one tab, and then you can close it.

Strange thing is that when you run:

    Stop-Process -Name WindowsTerminal -Force -ErrorAction SilentlyContinue  

when you just have the terminal open, no problem.

Now, it gets even stranger. When you comment out the line to kill the CmdProcess.ID ... The batch file keeps running... Even adding:

Stop-Process -Name cmd -Force -ErrorAction SilentlyContinue

doesn't help.

I don't know if this is a Powershell bug or a terminal bug or both...?

zadjii-msft commented 9 months ago

Yea this is a bit of a tricky one we're tracking in #15936. The easiest fix to get out of this state is to open a new tab, then close the window that way. You can follow #15936 for more updates. Thanks!

/dup #15936

microsoft-github-policy-service[bot] commented 9 months ago

Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report!