by stocksp at 2012-09-18 09:28:05
I need to only allow one copy of my script to run, so I want to check as the first step to see if there are any other PS processes running and terminate them.by stocksp at 2012-09-18 11:44:25
(Get-WmiObject win32_process -Filter "name = ‘powershell.exe’").Terminate()
works great from killing the window I’m in.
How can I terminate ‘others’ but not me?
I have a simple bat file I want the user to able to run as many times as they like. only one copy of the script runs.
Bat file is
powershell.exe -noexit -WindowStyle Minimized C:\scripts\fileWatcherDD.ps1
I got this working withby poshoholic at 2012-09-18 21:00:56
(Get-WmiObject win32_process -Filter "name = ‘powershell.exe’") | foreach{if($.ProcessId -ne $pid){$.Terminate()}}
Excellent, glad you worked it out.by RichardSiddaway at 2012-09-19 05:26:02
I always like a WMI solution but you could also try this
Get-Process -Name powershell | where {$_.Id -ne $pid} | Stop-Process
If you want to stick with WMI you could simplify to
Get-WmiObject -Class Win32_Process -Filter "Name=‘powershell.exe’ AND ProcessId != $pid" | Invoke-WmiMethod -Name Terminate