Page 1 of 1

Closing CMD windows with PowerShell [SOLVED]

Posted: 24 Jul 2023 17:38
by DOSadnie
Is there way to close all CMD windows [including hidden ones] using strictly PowerShell code [thus not inserting to a PS1 file the >>taskkill<< batch command]?

These do not work

Code: Select all

Stop-Process -Name cmd.exe -Force

Code: Select all

Stop-Process -Name CMD -Force
even when executed with privileges

Re: Closing CMD windows with PowerShell

Posted: 24 Jul 2023 20:31
by Batcher

Code: Select all

(Get-Process cmd).Kill()

Re: Closing CMD windows with PowerShell

Posted: 25 Jul 2023 09:53
by DOSadnie
This works for a CMD window that I open manually - i.e. by executing

C:\Windows\System32\cmd.exe

or

C:\Windows\SysWOW64\cmd.exe

But it leaves opened all CMD windows that were evoked by executing of some BAT files and which are paused after displaying of some text

Re: Closing CMD windows with PowerShell

Posted: 09 Aug 2023 04:32
by DOSadnie
I have found the culprit: it is an active countdown in CMD window that is making it immune to being closed

Even a BAT as simple as this one

Code: Select all

@echo off
timeout /t 10
pause
will not be closed until the timeout is finished, even if PS1 is using both methods

Code: Select all

Stop-Process -Name cmd -Force
(Get-Process cmd).Kill()
and is being run as Administrator while BAT without privileges


But solution to this seems to be using of either

Code: Select all

Stop-Process -Name timeout -Force
or

Code: Select all

(Get-Process timeout).Kill()
as apparently CMD window with active timeout in it changes its name to such. This works even if BAT has been executed as Administrator while PS1 was not


Thank you for suggesting usage of the Get-Process approach

Re: Closing CMD windows with PowerShell

Posted: 18 Sep 2023 06:47
by DOSadnie
DOSadnie wrote:
09 Aug 2023 04:32
I have found the culprit: it is an active countdown in CMD window that is making it immune to being closed
[...]
as apparently CMD window with active timeout in it changes its name to such
[...]
And I have discovered a second [unrelated] case in which using of the

Code: Select all

timeout
command creates an issue: viewtopic.php?f=3&t=10897&p=69035#p69035