DEV Community

Petr Janik
Petr Janik

Posted on

Kill process on port on Windows

I would like to create an alias for finding process on specified port and an alias to kill process by PID.

  1. Create an alias.cmd file in %USERPROFILE%

    notepad alias.cmd

    then add the following three lines to the file:

    @echo off
    DOSKEY proc=netstat -ano ^| findstr :$1
    DOSKEY kill=taskkill /F /PID $1

    The first alias proc accepts one argument and lists PID of process running on that port.
    The second - kill (which also accepts one argument) - terminates the process by PID.

  2. Run regedit and go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor (for MS10), HKEY_CURRENT_USER\Software\Microsoft\Command Processor (for < MS10). Add String Value entry with the name AutoRun and value %USERPROFILE%\alias.cmd (path to .cmd file).

Now I can start cmd, type proc 8080 and then for example kill 15003.

Top comments (0)