Windows – How to make a .bat file run only in system tray

batch filecommand-line-argumentsnetworkingwindows 10wireless-networking

TL;DR at the bottom!

Details here:

I have an unreliable internet connection due to 2 reasons on my Win 10 laptop, which is connected wirelessly to my router, which is connected by wire to my modem.

  • Reason 1, something that isn't Win 10's fault:
    My modem often fritzes out (IDK if it's the modem itself, the ISP, or what, but yeah), whereby it kinda just restarts. When this happens, the internet on my laptop (obviously) goes out too, but it then doesn't reconnect when the modem comes back on. The only way it'll do so is if I disable, then enable, my wireless adapter from the Network Connections window.

  • Reason 2, something that is Win 10's fault:
    My laptop sometimes gets kicked off the Wi-Fi network. This itself might be caused by something other than Win 10, like my NIC, or my router. What annoys me is when Win 10 then doesn't automatically reconnect to the network even though it's supposed to. When I click on the "Connect" button in the Wi-Fi menu myself, it works fine.

I decided to make a *.bat file that would detect whenever either of the scenarios occurred, and then fix the problem. With a little bit of research on the internet, I managed to put together the following script:

@echo off
netsh interface set interface "<wireless adapter>" disabled
netsh interface set interface "<wireless adapter>" enabled
netsh wlan connect name="<ssid>" interface="<wireless adapter>"
timeout /t 15
goto LOOP

This works great (when it's Run as Administrator), but now the problem is that I want the cmd window to not remain open (on screen, and in the taskbar) while the process runs; I want it to run in the tray instead, so I can still easily exit it if I need to, without having to go to the Task Manager. I know I can schedule the script in the Task Scheduler to start minimized on start-up, but won't that just hide the window from taskbar?


How do I make a *.bat file run in the tray / by the clock where I can easily right-click it and stop/exit?
Is compiling an executable the only way to achieve this?

Best Answer

Since I don't believe this can be done through a simple .bat file, this seem like a job for AutoHotKey.

AutoHotkey (AHK) is a free, open-source macro-creation and automation software for Windows that allows users to automate repetitive tasks. It is driven by a scripting language that was initially aimed at providing keyboard shortcuts, otherwise known as hotkeys, that over time evolved into a full-fledged scripting language.

According to this forum post from 2014 you should be able to toggle window visibility with a script similar to this:


global hBatFile

/* Setup Tray icon and add item that will handle
* double click events
Menu Tray, Icon
Menu Tray, Icon, C:\windows\system32\cmd.exe
Menu Tray, Add, Show / Hide Pingu, TrayClick
Menu Tray, Add, Close Pingu, CloseItem
Menu Tray, Default, Show / Hide Pingu

;// Run program or batch file hidden
DetectHiddenWindows On
Run pingu.bat,, Hide, PID
WinWait ahk_pid %PID%
hBatFile := WinExist()
DetectHiddenWindows Off


;// Show / hide program or batch file on double click
OnTrayClick() {
    if DllCall("IsWindowVisible", "Ptr", hBatFile) {
        WinHide ahk_id %hBatFile%

    } else {
        WinShow ahk_id %hBatFile%
        WinActivate ahk_id %hBatFile%

CloseItem() {

       DetectHiddenWindows On
       WinWait ahk_class ConsoleWindowClass
       Process, Close, cmd.exe
       DetectHiddenWindows Off


This script was tested with AutoHotKey (May 2016).

EDIT: Here is a link to the OPs modified version with improvements.

In this instance:

  • pingu.bat is the (arbitrary) name of the batch file we wish to run
  • hBatFile is an arbitrary variable name holding some window information
  • Pingu is an arbitrary name which shows in the additional AHK tray menu items

Simply save the script as something like tray.ahk (note the .ahk script extension) and double click to run it (assuming you have installed AutoHotKey).

The script starts the batch file minimized and replaces the default AHK script icon with a miniature console window icon in the tray.

Console window visibility can be toggled with a double click of the tray icon or the added tray menu item (appearing in bold at the bottom of the selection menu).

Closing both the batch file and the command window can be done with the added Close menu item below Show / Hide (Exit will only close the script, not the console window).

Also note that

Run pingu.bat,, Hide, PID

will obviously need pingu.bat to be replaced with the name of your own batch file. This line assumes that the AHK script appears in the same directory as pingu.bat or that pingu.bat is globally accessible (i.e. it has been added to a folder in your system Path or user PATH variables). Otherwise, you will want to replace pingu.bat with the full path to the executable (watch for spaces!).


This script mostly acts as an interface to window visibility. What this means is that if you Exit via the tray icon, you are only exiting the AHK script, not your batch file.

The simple solution is to use the added Close menu item to exit both the script and stop cmd.exe. Alternatively, you can:

  • Show the console window and use Ctrl + C (or simply close the console window with the red 'X') to terminate the batch process.

  • Select Exit from the tray icon to stop the AHK script

They are separate processes, as mentioned.

Note: Orphaned console programs with hidden windows cannot be directly accessed again if the AHK script is terminated with Exit first — but you can use Task Manager to close the associated cmd.exe process.

The Close menu item as written works fine if there is only one cmd.exe process. However, if more than one cmd.exe process is running, this may not properly close the batch file you started with the script (it may close something else). You may want to look into closing by process ID (PID) instead. That said, you can use the same Show/Close/Exit process above as well.

Script Notes

Menu Tray, Icon, C:\windows\system32\cmd.exe

refers to the location of a compiled executable with an associated embedded icon file. You should be able to replace this with a reference to any executable with an embedded icon or with a direct reference to an .ico icon file as well e.g.

Menu Tray, Icon, C:\Path\To\Icons\icon.ico