Enzo Nassif
Enzo Nassif

Reputation: 13

Powershell kill all Powershell windows that are open besides the current one

Get-Process powershell | Format-table -Property Id -HideTableHeaders -Force | out-string -OutVariable "a"
foreach ($a in $tre) {stop-process $tre}

Upvotes: 0

Views: 289

Answers (2)

Enzo Nassif
Enzo Nassif

Reputation: 13

$rtrtrt = (get-random)

Get-Process powershell | Format-table -Property name, Id -HideTableHeaders -OutVariable "zaz"

$zaz = $zaz | out-string

$zaz = $zaz -replace " ", " "

$zaz = $zaz -replace " ", " "

$zaz = $zaz -replace "powershell $pid", ""

$zaz=$zaz -replace "powershell", "stop-process -force"

echo $zaz >$env:temp\$rtrtrt.ps1

&"$env:temp\$rtrtrt.ps1"

IF(!(Test-Path "$env:temp\$rtrtrt.ps1")) {echo "#"} else {Remove-Item "$env:temp\$rtrtrt.ps1" -recurse}

I made that up but yours looks much better thank you

enter code here

I'm gonna use yours obviously

Get-Process powershell | Where-Object ID -ne $PID | Stop-Process

Upvotes: 0

user6811411
user6811411

Reputation:

It is much simpler, your script fragment foreach ($a in $tre) has the wrong order,
should be foreach ($tre in $a) but that wouldn't exclude the current powershell.

Try this:

Get-Process powershell | Where-Object ID -ne $PID | Stop-Process

Upvotes: 2

Related Questions