How do you close a program as soon as you open it using a batch file? - command-line

I am trying to close firefox as soon as I open it, and then reopen it again in a batch file.
This is what i have below:
#ECHO off
"C:\Program Files\Mozilla Firefox\firefox.exe"
sleep 10
taskkill /F/IM "C:\Program Files\Mozilla Firefox\firefox.exe"
sleep 10
"C:\Program Files\Mozilla Firefox\firefox.exe"
However it doesnt work, it just opens firefox and never closes it.
The system I am trying to get this to work on is Windows 7 Home Premium 64bit. I don't know if that makes much of a difference.
Any help on this will be greatly appreciated. Thanks.

Taskkill is used to kill the process.
Try
taskkill /F/IM firefox.exe
Here is the documentation from MSDN
http://technet.microsoft.com/en-us/library/bb491009.aspx

Related

Is there a way to open a CMD window with a running command through Powershell?

I know it is possible to use a batch file, but due to a suggestion I switched a lot of my script over to PowerShell. The problem I ran into is that PowerShell is still missing some commands from CMD and has lower permissions when run as an Admin. Below is the current line I have been using.
powershell -Command "Start-Process 'cmd' -Verb RunAs -ArgumentList 'del /s "C:\Users\*.mp3"'"
I ran it in both 5.1 and 7.
It works with a simple command like opening the calculator or sending a ping, but I can't get the del command to work. The goal is to open a CMD window which will then delete all mp3 files. I know there are probably better ways to do this, but I more so want to know if it's possible than efficient. Thank you for your time!
This works somewhat
Start-Process cmd "/c del /S C:\Users\*.mp3"

Is there a way to switch and close a specific tab on Chrome via Command-line or VBScript? [duplicate]

I'm having a little problem about closing gently Chrome in order to automatically clean cache.
I have a website that is not refreshing correctly, but it does if I clean the cache.
For now I have a .bat file with the following code:
taskkill /F /IM chrome.exe /T > nul
del /q "C:\Users\Francisco\AppData\Local\Google\Chrome\User Data\Default\Cache\*"
FOR /D %%p IN ("C:\Users\Francisco\AppData\Local\Google\Chrome\User Data\Default\Cache\*.*") DO rmdir "%%p" /s /q
timeout 8
start chrome --restore-last-session --start-fullscreen
Now, I know that taskkill /F forces the process to close and that works but as soon as Chrome opens, it shows a message that Chrome wasn't closed correctly and asks me if I want to restore the last session.
If i remove the /f option, Chrome doesn't close:
ERROR: the process with PID 8580 (PID secondary process 8896)
Could not finish.
Reason: This process can be terminated only forcibly (with the / F option).
So... I need one of two options;
Is there a way to gently close Chrome like pressing Alt+F4 using CMD or a VBS?2. Is there a way to hide that Chrome restore last session message?
I'd prefer the first option since it's cleaner.
you can use powershell to close all windows with chrome as process name. It won't kill the task but gently close all chrome browser windows, like if you clicked on top-right cross. Here is the command line to put in batch :
powershell -command "Get-Process chrome | ForEach-Object { $_.CloseMainWindow() | Out-Null}"
Refer to the answer of #tuxy42 :
You can write with vbscript to open chrome browser like this : Open_Chrome_Browser.vbs
Set ws = CreateObject("Wscript.Shell")
siteA = "https://stackoverflow.com/questions/62516355/google-chrome-always-says-google-chrome-was-not-shut-down-properly"
ws.Run "chrome -url " & siteA
And if you want to close it safely : safely_close_chrome_browser.vbs
Set ws = CreateObject("Wscript.Shell")
psCommand = "Cmd /C powershell -command ""Get-Process chrome | ForEach-Object { $_.CloseMainWindow() | Out-Null}"""
ws.run psCommand,0,True
Just browse URL
chrome://quit/

Powershell Background if Ping script

I have an old CMD Script which I want to convert into a Powershell Script which runs in the Background, the CMD Script:
#echo off
timeout /t 300 /nobreak
ping -n 1 -i 135 -w 130 192.168.1.250
if errorlevel 1 goto Ende
goto Program
:Program
start "" "C:\Program Files (x86)\XXX\XXX.exe"
Exit
:Ende
Exit
The Script checks if the Router is pingable, if yes it starts a program.
I have completly zero experience with either CMD or Powershell(see Goto <.<), I tried to do it with google and found some solutions to run it in background but they didn't really work out for me.
The Sleep option should work with start-sleep -s 300, the Ping Check with if (test-connection -computername Server01 -quiet) but im not completly sure how to do an if-statement around the ping.
Greetings
I'd try this:
Ping-AndLaunch.ps1:
Start-Sleep -Seconds 300
if(Test-Connection "192.168.1.250" -Count 1 -TimeToLive 135) {
& "C:\Program Files (x86)\XXX\XXX.exe"
}
Launch command (to hide the PowerShell window):
powershell -File .\Ping-AndLaunch.ps1 -WindowStyle Hidden
This is a post by IAMGr00t on community.spiceworks.com
"1. Deploy a policy that sets the execution policy to remote signing, you will have to sign your scripts. Have a look here on how to do that.
or
Make a .bat file that when launched will open powershell and run the command. The caveat here is that it is very important you get your switches correctly. If these need to be specific to the user running the script do NOT use -noprofile. If you want it to run silently in the background your batch file would look something like this.
powershell.exe -executionpolicy bypass -windowstyle hidden -noninteractive -nologo -file "name_of_script.ps1"
EDIT: if your file is located on another UNC path the file would look like this. -file "\server\folder\script_name.ps1"
These toggles will allow the user to execute the PowerShell script by double clicking a batch file. There will be no window, no copyright logo, and no user interactivity. The perks of this, is the user does not see the background noise. I have had to do this recently. It works without muddling with all users' execution policies.
Really makes you wonder how secure your environment is if you can run a script with -bypass flags. Also keep in mind things that would need administrative write access. But, what I have said above should point you in the right direction. Just make a .bat file with the one line of opening PowerShell. It feels clunky (it is) but it works."

Monitor ccmsetup.exe installation status usng powershell

This question is realted to SCCM Client installer ccmsetup.exe. When we run manually ccmsetup.exe; it starts the installation process and exit from command prompt. I cannot monitor the installation process from commandline. It starts recording installation status in ccmsetup.log file. However time to execute completely varies per system.
How can I monitor ccmsetup.exe installation status using powershell.
I am using command as below:
Invoke-Command { C:\Client\ccmsetup.exe /Source:"C:\Client" SMSSITECODE=PPR FSP=Server1.ADDOMAIN.COM}
Thanks & regards,
Kedar
Give a shot to the start-process cmdlet :
start-process C:\Client\ccmsetup.exe -ArgumentList #( '/Source:"C:\Client"' , "SMSSITECODE=PPR" , "FSP=Server1.ADDOMAIN.COM" -wait
And tell me it works, because it works for me :)
NB : the use a simple quotes allows double-quotes to be passed to the process launched.

Win7 runas command: How to capture output of command that is run?

I'm trying (under Windows 7) to use the runas command to stop then restart a service. (Win7 requires admin privs to do this; thus the use of runas.)
Stopping the service works fine, but starting it does not. Here's the command I'm using for stopping the service:
runas /user:myDomain\myUserId "net stop serviceName"
Here's the command for starting the service:
runas /user:myDomain\myUserId "net start serviceName"
When I run the above command another command window opens, but flashes away before I can see anything in it; thus I have no idea what's going wrong.
So, my question is: How might I capture stdout and/or stderr from the net start command when run via runas? I've tried just using redirection but just get an empty file. Another solution would be to get the window opened by runas for the subtask to remain open.
Thanks in advance.
Launch cmd.exe instead with the command to run, and specify that the output be written to a file.
runas /user:myDomain\myUserId "cmd.exe /c net stop serviceName > output.txt"
You can use 2> for error output from net stop.
Also, if you don't want to bother with the output file, you can use cmd.exe /k instead of /c to launch the command and it will leave the session window open for you. Might be easier/quicker if you just want a quick peek.
Try gsudo and run the command:
gsudo net stop serviceName