0

I want to make it such that when a user runs a specific program, such as Firefox, my batch file starts in the background.

I used the code below, but it makes my batch file start instead of Firefox. I don't want that. I want the batch file to listen to programs in the computer and when a certain program starts, the batch file will start in background.

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\firefox.exe" /v Debugger /d "D:\Desktop\m.bat" /f

How can I accomplish this?

  • Do you really want your batch file to run whenever any program starts (as stated in your question title)? Or do you want your batch file to run when only specific programs run? – I say Reinstate Monica Apr 22 '17 at 13:58
  • when only specific program like firefox – Muhammad Khaled Apr 22 '17 at 15:23
  • @Twisty when only specific program like firefox – Muhammad Khaled Apr 22 '17 at 16:18
0

You can use this PowerShell script:

$query = New-Object System.Management.WqlEventQuery ("__InstanceCreationEvent", (New-Object TimeSpan (0, 0, 1)), 'TargetInstance isa "Win32_Process"')
$watcher = New-Object System.Management.ManagementEventWatcher
$watcher.Query = $query
$watcher.Options.Timeout = [System.Management.ManagementOptions]::InfiniteTimeout
$curProc = $null
While ($true) {
    $e = $watcher.WaitForNextEvent().TargetInstance
    If ($e.Name -eq 'firefox.exe' -and ($curProc -eq $null -or $curProc.ExitTime -ne $null)) {
        $curProc = Start-Process 'cmd' -Argument '/c C:\path\to\script.bat' -PassThru -WindowStyle Hidden
    }
}

It uses WMI to watch for the creation of new processes, and if the process belongs to firefox.exe, it launches a hidden command prompt (unless one is already running from a previous Firefox start).

If you want a new run of the batch file to be started every time Firefox launches, use this simpler script:

$query = New-Object System.Management.WqlEventQuery ("__InstanceCreationEvent", (New-Object TimeSpan (0, 0, 1)), 'TargetInstance isa "Win32_Process"')
$watcher = New-Object System.Management.ManagementEventWatcher
$watcher.Query = $query
$watcher.Options.Timeout = [System.Management.ManagementOptions]::InfiniteTimeout
While ($true) {
    $e = $watcher.WaitForNextEvent().TargetInstance
    If ($e.Name -eq 'firefox.exe' ) {
        Start-Process 'cmd' -Argument '/c C:\path\to\script.bat' -WindowStyle Hidden
    }
}

If you don't want the prompt window to be completely hidden, just change -WindowStyle Hidden to -WindowStyle Minimized.

Save your selected PowerShell script as a .ps1 file. To start it at your logon, place a batch file containing this in your startup folder:

powershell -file 'C:\path\to\powershellScript.ps1' -executionpolicy bypass -windowstyle Hidden
|improve this answer|||||
  • thank you very much but the code not working I copied your code and i changed the parameters but no thing had been occurred – Muhammad Khaled Apr 23 '17 at 12:14
  • I have this error You cannot call a method on a null-valued expression. At D:\power.ps1:6 char:35 + $e = $watcher.WaitForNextEvent <<<< ().TargetInstance + CategoryInfo : InvalidOperation: (WaitForNextEvent:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull – Muhammad Khaled Apr 23 '17 at 13:14
  • @MuhammadKhaled Interesting, it works for me on Windows 10 x64. What version of Windows do you have? It's possible that the special ::new syntax is not supported on the version of PowerShell that comes with your version. – Ben N Apr 23 '17 at 18:21
  • @MuhammadKhaled I've updated my scripts to use the older New-Object approach. Does it work now? – Ben N Apr 23 '17 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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