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