Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to do a script that'll allow me to kill a few processes when I need a maximum of resources. Things I don't mind starting up when I start XP (dropbox, PowerISO, OpenOffice-tray, etc.) but since they all eat a little memory each I'd like to create a script that terminates them all by one click.

Now my problem is SQL-related. I have a script that will terminate a single process looking like this:

       strComputer = "."
       Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
       Set colProcessList = objWMIService.ExecQuery _
        ("SELECT * FROM Win32_Process WHERE Name = 'PWRISOVM.EXE'")
       For Each objProcess in colProcessList
        objProcess.Terminate()
       Next

But I can't, for the life of me, make the SELECT statement grab more than a single process. If anybody can enlighten me on this I'll be very happy :-)

share|improve this question

2 Answers 2

up vote 2 down vote accepted

It's WQL ("SQL for WMI"). This is important to note, because they are not identical. WQL is intended to be close to SQL. :) Anyhow...

SELECT * FROM Win32_Process WHERE Name = 'PWRISOVM.EXE'

Will only select processes with the specific name 'PWRISOVM.EXE'.

Select * from Win32_Process

Would get you a full list.

I made a similar script for Vista/7 a few years ago. Make an array of processes to keep, and then cycle through killing processes with no dependants, skipping the ones in the array. Then do it again, this will help kill all children first, and then parents. Third pass should "ignore if they have dependants" and "force" closure; adjust your loop amounts as you see fit. :)

Do the similar for Services ("Stop" them before killing processes).

From my experiences, you'll find the most resource recovery by just killing Explorer.exe alone.

And don't forget your Task Manager, CMD, and scripting host process exclusions. ;)

-- Edit: --

With your example code, if you use just Select * from Win32_Process it will put the full list into the collection "colProcessList". As you cycle through them all in your "For Each" loop, you can decided on a process by process basis.

To directly answer what your comment seems to ask for:

To select multiple, specific processes by name in one Select statement, use the 'OR' operator:

Select * from Win32_Process where Name = 'Process1' or Name = 'AnotherProcess'

share|improve this answer
    
Right. Thank you @techie007 - but what I am looking for is a method to SELECT a list of MULTIPLE processes at once so I would get a hold of 'PWRISOVM.EXE' and 'Dropbox.exe' - and so on. What I aim to do is have a script that I run when I need every muscle on ONE project that terminates all the superfluous processes. I thought I could just do ("SELECT 'PWRISOVM.EXE', 'Dropbox.exe' FROM Win32_Process") or ("SELECT * FROM Win32_Process WHERE Name = 'PWRISOVM.EXE', 'Dropbox.exe'") But it doesn't work and I get error messages! –  morganF Jan 17 '12 at 23:52
    
I'll update my answer a bit. –  Ƭᴇcʜιᴇ007 Jan 17 '12 at 23:55
    
SUPER! The SELECT statement in the bottom of your revised comment is exactly what I was looking for! BTW. I was trying to add 'WQL ' as a tag for my question but it didn't come up in suggested tags. –  morganF Jan 18 '12 at 1:19
1  
I feel this is the simpler solution - I know what processes I need to target (or most of them anyway) and I would just like a quick way to do all that in a single click. Now I can :-) –  morganF Jan 18 '12 at 1:21
    
NP, I just over-thought your question, I think. ;) –  Ƭᴇcʜιᴇ007 Jan 18 '12 at 2:48

If it came down to it... I would just put them in line... one process after the other... in the exact same format you have there.

   strComputer = "."
   Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   Set colProcessList = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Process WHERE Name = 'PWRISOVM1.EXE'")
   For Each objProcess in colProcessList
    objProcess.Terminate()
   Next

   strComputer = "."
   Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   Set colProcessList = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Process WHERE Name = 'PWRISOVM2.EXE'")
   For Each objProcess in colProcessList
    objProcess.Terminate()
   Next

3,4,5 etc.

share|improve this answer
1  
why is this better than Select * from Win32_Process where Name = 'Process1' or Name = 'AnotherProcess' as listed in the accepted answer? –  Jeremy W May 30 '12 at 2:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.