2

I need a CMD command in Windows to switch to an already open application. Something like Alt + Tab. But if Alt + Tab, he may switch to another application that is not right.

If I have three open applications, I would like the command you switch to a specific.

3
  • You need to describe first what your "trying to do" fully. Is this via a batching? Is it because you wish to Not run a second instance? Are you intending to type this in the cmd or use batches, or icons? I use the "PrcView" (command line utility by Igor Nys) and batch to have the same (batch) Icon, pv.exe -a "activate" a program that is already running, or (error level) to run a program that is not running . It is very convienient. There are many other programs that can be used in scripts to check first.
    – Psycogeek
    Commented Sep 11, 2015 at 12:51
  • 1
    possible duplicate of Application to automatically switch between two applications in Windows Commented Sep 12, 2015 at 10:33
  • @Psycogeek: The question is asking for Win CMD commands, which may be via batch file (the question has nothing to do with icons) to "switch to" or make a program (that is already running) window active.
    – Zimba
    Commented Feb 29, 2020 at 4:09

5 Answers 5

4

How do I switch to an already open application from the command line?

You can use nircmd from nirsoft to do this.

Example:

nircmd win activate title "Calculator"

Notes:


What is nircmd?

NirCmd is a small command-line utility that allows you to do some useful tasks without displaying any user interface. By running NirCmd with simple command-line option, you can write and delete values and keys in the Registry, write values into INI file, dial to your internet account or connect to a VPN network, restart windows or shut down the computer, create shortcut to a file, change the created/modified date of a file, change your display settings, turn off your monitor, open the door of your CD-ROM drive, and more...

Source nircmd


Disclaimer

I am not affiliated with nirsoft in any way, I am just an end user of the software.

1
  • Thanks, NirCMD will work too, I found other solution too: AutoIt. :) Commented Sep 11, 2015 at 13:20
2

The solution with 1 line of initialisation and 1 line calling. The calling is %tmp%\switch.vbs ... used twice below.

Title %vTitle%
:: preparation (initialisation)
@echo WScript.CreateObject("WScript.Shell").AppActivate(WScript.Arguments.Item(0))>%tmp%\switch.vbs
:: some cmd lines exposing other windows
:: ...
:: just as an example to start the notepad
start "" notepad
:: use 1: focus back on this cmd window
%tmp%\switch.vbs %vTitle%
:: ...
:: use 2: focus to an application with <application name> 
%tmp%\switch.vbs "<application name>"
0

Other possible solution: AutoIt:

Solution found at: Application to automatically switch between two applications in Windows

:)

0

Win CMD, batch files & DOS are command line (CLI) utilities, and if you're looking at switching application windows, you need scripts that work with GUI.

VBScript comes "installed by default in every desktop release of Microsoft Windows since Windows 98" and "gained support from Windows system administrators seeking an automation tool more powerful than the batch language".

JScript has been supported since then too, and has syntax similar to Javascript, "Microsoft's dialect of the ECMAScript".

Either VBS or JS codes can be inserted into & run from batch files, and hosted by either cscript (console) or wscript (window).

To "switch to an already open application" via Win CMD:

Save this as switch.vbs

Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Dim ARGS
set ARGS=WScript.Arguments

WshShell.AppActivate(ARGS.Item(0))
WshShell.SendKeys("~")
WScript.Echo(ARGS.Item(0) + " activated")
WScript.Quit(0)

Then switch.vbs "<application name>"

or

Save this as switch.bat

@if (@a==@a) @end /*
        cscript //E:JScript //nologo "%~f0" %*

    REM --- Insert other batch codes here ---

        exit /b 
*/

// --- JScript codes below this line ----

var WshShell = WScript.CreateObject("WScript.Shell");
var ARGS = WScript.Arguments;

WshShell.AppActivate(ARGS.Item(0));
WshShell.SendKeys("~");
WScript.Echo(ARGS.Item(0) + " activated");
WScript.Quit(0);

Then run switch.bat "<application name>"

If you don't want to create a separate file like above solutions or download external programs like nircmd or nirsoft to run from Win CMD (ie. a solution that can be effected from Win CMD only ), then either echo to temp file then delete after calling, or store all commands in a variable then command the variable.

Tested in Win 10

0

Here is a cmd one-liner with no external dependency. It is designed to start or bring-to-front Microsoft Edge, which has multiple processes and we have to find the correct one first (the one with a MainWindowHandle).

powershell -command "$edgeProcess = Get-Process -Name msedge -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowHandle -ne 0 }; if ($edgeProcess) { (New-Object -ComObject wscript.shell).AppActivate($edgeProcess.Id) } else { Start-Process msedge }"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .