Closing CMD windows with PowerShell [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Closing CMD windows with PowerShell [SOLVED]

#1 Post by DOSadnie » 24 Jul 2023 17:38

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
Last edited by DOSadnie on 11 Aug 2023 02:28, edited 3 times in total.

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Closing CMD windows with PowerShell

#2 Post by Batcher » 24 Jul 2023 20:31

Code: Select all

(Get-Process cmd).Kill()

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Closing CMD windows with PowerShell

#3 Post by DOSadnie » 25 Jul 2023 09:53

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

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Closing CMD windows with PowerShell

#4 Post by DOSadnie » 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

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

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Closing CMD windows with PowerShell

#5 Post by DOSadnie » 18 Sep 2023 06:47

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

Post Reply