How to terminate a process in vbscript - vbscript

How can I terminate process using vbscript. PLEASE NOTE, I need to terminate process that runs under windows 64-bit environment as native 64 (not using select * from win_32_Process)
Thanks,

The Win32_Process class provides access to both 32-bit and 64-bit processes when the script is run from a 64-bit command shell.
If this is not an option for you, you can try using the taskkill command:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
' Launch notepad '
oShell.Run "notepad"
WScript.Sleep 3000
' Kill notepad '
oShell.Run "taskkill /im notepad.exe", , True

Just type in the following command: taskkill /f /im (program name)
To find out the im of your program open task manager and look at the process while your program is running. After the program has run a process will disappear from the task manager; that is your program.

I know I am late to the game LOL, but this is what I did to kill SmartScreen.
Create text file KillSmartScreen.vbs
Edit with Notepad:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
'Kill smartscreen.exe aka Windows Defender SmartScreen
oShell.Run "taskkill /f /im smartscreen.exe"
Double click KillSmartScreen.vbs to run.
Works like a champ.

Related

How to end a file using vbs without starting anything?

So, I'm trying to make a security system using vbs that would terminate a process.
Ex:
If you see "cmd.exe"
Terminate cmd.exe
I have coded everything except for the terminating part. I have searched many places but all they do is first run that file and then terminate it. I want to terminate the existing running "cmd.exe"
I tried running a batch file that terminates all "cmd.exe" processes but it just terminated itself leaving the other "cmd.exe" open.
Powershell is not an option too because it's one of the process I want to terminate.
Regards,
A Viper
Running TaskKill Command with VB Script
TaskKill command can terminate all existing processes whose running in same image name.
For example, if the process you want to be terminated is "cmd.exe", you can use TaskKill as below in your script:
Dim WshShell : Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "CMD /C TASKKILL /F /IM cmd.exe", 0, False
Above script is executed using Windows Script Host, which is not a process you want to be terminated, so this should work.

How to close last tab in firefox using windows command line

I am creating a batch file to handle firefox tabs.
I want to close the last tab in firefox using windows command.
I have tried this command using title name
taskkill /IM firefox.exe /FI "WindowTitle eq localhost*"
It closes all the window
I have searched in windows command, there are no direct way.
Is there any possibilities ?
You will have to use VBScript code to send Ctrl+w keys to Firefox.
Save this code as closeActiveTab.vbs and run it with cscript closeActiveTab.vbs in your batch file or embed the code inside.
Dim Shell, WMI, query, process
Set Shell = CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2")
query = "SELECT ProcessId FROM Win32_Process WHERE Name = 'firefox.exe'"
For Each process In WMI.ExecQuery(query)
Shell.AppActivate process.ProcessId
WScript.Sleep 100
Shell.SendKeys "^w"
Next
Based on another answer.

Is there a way to start a program minimized with VBScript using WScript.Shell?

Here is some example code I have right now to launch an app:
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run """C:\Program Files\Handbrake\HandBrakeCLI.exe"""
I tried the following to launch the app minimized but it didn't work. I'm assuming this only works from a normal command prompt?
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "start /MIN ""C:\Program Files\Handbrake\HandBrakeCLI.exe"""
I've also tried launching a shortcut (which just gave a null error and the script couldn't run) as well as trying to do a sendkeys to minimize it which didn't work at all.
This is from a VBScript running via cscript.exe by the way.
Does anyone know how I can start this app minimized within VBScript?
Check the docs and use the second parameter of the .Run method.
Evidence:
set s = createobject("WScript.Shell")
s.run "notepad", 2
starts Notepad minimized.

VBScript- Single line as administrator

Is it possible to use the shell.run command to run the specified program as an administrator?
for instance:
shell.run(cmd.exe) <---Run this as admin
shell.run(Notepad) <---Run this as normal
I know i can execute the script to run as admin but that means everything inside that script is executed as an administrator.
My other option was to seperate the scripts and run one as admin and include what needs to be ran as admin in that script, then call another script to run and run that one as normal.
Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "cmd.exe", , , "runas", 1
oShell.Run "nodepad.exe"
(Source: http://social.technet.microsoft.com/Forums/eu/ITCG/thread/310e57f9-2367-4293-9f97-53d0e077aacc)
(More Info: http://ss64.com/vb/shellexecute.html)
Windows (from XP through to Win7 at least) has a runas command which does what you need. See here for details.
So, instead of running cmd.exe, you would run runas.exe, giving cmd.exe as the program to run.
Try this:
Dim ObjShell
Set ObjShell = CreateObject ("WScript.Shell")
ObjShell.Run "runas /K (command goes here) "
ObjShell.Run "notepad.exe"

How to keep the VBScript command window open during execution

When I execute a VBScript, the command window that it creates closes quickly before the user gets a chance to read the output. How can I get the window to stay open without modifying windows registry?
This is the code:
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "SyncToyCmd.exe -R", 1, True
You can send your execution command through the cmd.exe command interpreter, along with a pause command which will give the user a Press any key to continue . . . prompt to close the window.
objShell.run "%comspec% /c ""SyncToyCmd.exe -R & pause""", 1, True
Or to keep the window alive, use the /k flag instead of /c:
objShell.run "%comspec% /k SyncToyCmd.exe -R", 1, True
But beware, your VBScript will not continue (or terminate) until this cmd window is manually closed.
The %comspec% environment variable refers to the correct command to open the command interpreter as per your operating system. On my XP machine, for instance, %comspec% is equal to C:\WINDOWS\system32\cmd.exe.
See cmd.exe documentation here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true
More info on the use of the & versus the && command separators here.
Assuming that it's the popped-up command window that you want to keep open (rather than the one running your VBScript), you can use CMD.exe's Pause command to achieve this:
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "cmd.exe /C ""SyncToyCmd.exe -R & Pause"" ", 1, True
Make it sleep for a while, maybe tell the user it will close in 5 seconds?
Set WScript = CreateObject("WScript.Shell")
WScript.Sleep 5000

Resources