Run / RunWait

Runs an external program. Unlike Run, RunWait will wait until the program finishes before continuing.

Run Target , WorkingDir, Options, &OutputVarPID
ExitCode := RunWait(Target , WorkingDir, Options, &OutputVarPID)



Type: String

A document, URL, executable file (.exe, .com, .bat, etc.), shortcut (.lnk), or system verb to launch (see remarks). If Target is a local file and no path was specified with it, A_WorkingDir will be searched first. If no matching file is found there, the system will search for and launch the file if it is integrated ("known"), e.g. by being contained in one of the PATH folders.

To pass parameters, add them immediately after the program or document name. For example, Run 'MyProgram.exe Param1 Param2'.

If the program/document name or a parameter contains spaces, it is safest to enclose it in double quotes (even though it may work without them in some cases). For example, Run '"My Program.exe" "param with spaces"'.


Type: String

The working directory for the launched item. Do not enclose the name in double quotes even if it contains spaces. If omitted, the script's own working directory (A_WorkingDir) will be used.


Type: String

If omitted, the function launches Target normally. To change this behavior, specify one or more of the following words:

Max: launch maximized

Min: launch minimized

Hide: launch hidden (cannot be used in combination with either of the above)

Note: Some applications (e.g. Calc.exe) do not obey the requested startup state and thus Max/Min/Hide will have no effect.


Type: VarRef

A reference to the variable in which to store the newly launched program's unique Process ID (PID). The variable will be made blank if the PID could not be determined, which usually happens if a system verb, document, or shortcut is launched rather than a direct executable file. RunWait also supports this parameter, though its OutputVarPID must be checked in another thread (otherwise, the PID will be invalid because the process will have terminated by the time the line following RunWait executes).

After the Run function retrieves a PID, any windows to be created by the process might not exist yet. To wait for at least one window to be created, use WinWait "ahk_pid " OutputVarPID.

Return Value

Type: Integer

Unlike Run, RunWait will wait until Target is closed or exits, at which time the return value will be the program's exit code (as a signed 32-bit integer). Some programs will appear to return immediately even though they are still running; these programs spawn another process.

Error Handling

If Target cannot be launched, an exception is thrown (that is, an error window is displayed) and the current thread is exited, unless the error is caught by a Try/Catch statement. For example:

    Run "NonExistingFile"
    MsgBox "File does not exist."

The built-in variable A_LastError is set to the result of the operating system's GetLastError() function.


When running a program via ComSpec (cmd.exe) -- perhaps because you need to redirect the program's input or output -- if the path or name of the executable contains spaces, the entire string should be enclosed in an outer pair of quotes. In the following example, the outer quotes are highlighted in yellow:

Run A_ComSpec ' /c ""C:\My Utility.exe" "param 1" "second param" >"C:\My File.txt""'

Performance may be slightly improved if Target is an exact path, e.g. Run 'C:\Windows\Notepad.exe "C:\My Documents\Test.txt"' rather than Run "C:\My Documents\Test.txt".

Special CLSID folders may be opened via Run. For example:

Run "::{20d04fe0-3aea-1069-a2d8-08002b30309d}"  ; Opens the "My Computer" folder.
Run "::{645ff040-5081-101b-9f08-00aa002f954e}"  ; Opens the Recycle Bin.

System verbs correspond to actions available in a file's right-click menu in the Explorer. If a file is launched without a verb, the default verb (usually "open") for that particular file type will be used. If specified, the verb should be followed by the name of the target file. The following verbs are currently supported:

Verb Description
*verb Any system-defined or custom verb. For example: Run "*Compile " A_ScriptFullPath. The *RunAs verb may be used in place of the Run as administrator right-click menu item.

Displays the Explorer's properties window for the indicated file. For example: Run 'properties "C:\My File.txt"'

Note: The properties window will automatically close when the script terminates. To prevent this, use WinWait to wait for the window to appear, then use WinWaitClose to wait for the user to close it.

find Opens an instance of the Explorer's Search Companion or Find File window at the indicated folder. For example: Run "find D:\"
explore Opens an instance of Explorer at the indicated folder. For example: Run "explore " A_ProgramFiles.
edit Opens the indicated file for editing. It might not work if the indicated file's type does not have an "edit" action associated with it. For example: Run 'edit "C:\My File.txt"'
open Opens the indicated file (normally not needed because it is the default action for most file types). For example: Run 'open "My File.txt"'.
print Prints the indicated file with the associated application, if any. For example: Run 'print "My File.txt"'

While RunWait is in a waiting state, new threads can be launched via hotkey, custom menu item, or timer.

Run as Administrator

For an executable file, the *RunAs verb is equivalent to selecting Run as administrator from the right-click menu of the file. For example, the following code attempts to restart the current script as admin:

full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
        if A_IsCompiled
            Run '*RunAs "' A_ScriptFullPath '" /restart'
            Run '*RunAs "' A_AhkPath '" /restart "' A_ScriptFullPath '"'

MsgBox "A_IsAdmin: " A_IsAdmin "`nCommand line: " full_command_line

If the user cancels the UAC dialog or Run fails for some other reason, the script will simply exit.

Using /restart ensures that a single instance prompt is not shown if the new instance of the script starts before ExitApp is called.

If UAC is disabled, *RunAs will launch the process without elevating it. Checking for /restart in the command line ensures that the script does not enter a runaway loop in that case. Note that /restart is a built-in switch, so is not included in the array of command-line parameters.

The example can be modified to fit the script's needs:

AutoHotkey's installer registers the RunAs verb for .ahk files, which allows Run "*RunAs script.ahk" to launch a script as admin.

RunAs, Process functions, Exit, CLSID List, DllCall


Run is able to launch Windows system programs from any directory. Note that executable file extensions such as .exe can be omitted.

Run "notepad"

Run is able to launch URLs:

The following opens an internet address in the user's default web browser.

Run ""

The following opens the default e-mail application with the recipient filled in.

Run ""

The following does the same as above, plus the subject and body.

Run " is the subject line&body=This is the message body's text."

Opens a document in a maximized application and displays a custom error message on failure.

try Run("ReadMe.doc", , "Max")
if A_LastError
    MsgBox "The document could not be launched."

Runs the dir command in minimized state and stores the output in a text file. After that, the text file and its properties dialog will be opened.

RunWait A_ComSpec " /c dir C:\ >>C:\DirTest.txt", , "Min"
Run "C:\DirTest.txt"
Run "properties C:\DirTest.txt"
Persistent  ; Keep the script from exiting, otherwise the properties dialog will close.

Run is able to launch CLSIDs:

The following opens the recycle bin.

Run "::{645ff040-5081-101b-9f08-00aa002f954e}"

The following opens the "My Computer" directory.

Run "::{20d04fe0-3aea-1069-a2d8-08002b30309d}"

To run multiple commands consecutively, use "&&" between each.

Run A_ComSpec "/c dir /b > C:\list.txt && type C:\list.txt && pause"

The following custom functions can be used to run a command and retrieve its output or to run multiple commands in one go and retrieve their output.

MsgBox RunWaitOne("dir " A_ScriptDir)

MsgBox RunWaitMany("
echo Put your commands here,
echo each one will be run,
echo and you'll get the output.

RunWaitOne(command) {
    ; WshShell object:
    shell := ComObject("WScript.Shell")
    ; Execute a single command via cmd.exe
    exec := shell.Exec(A_ComSpec " /C " command)
    ; Read and return the command's output
    return exec.StdOut.ReadAll()

RunWaitMany(commands) {
    shell := ComObject("WScript.Shell")
    ; Open cmd.exe with echoing of commands disabled
    exec := shell.Exec(A_ComSpec " /Q /K echo off")
    ; Send the commands to execute, separated by newline
    exec.StdIn.WriteLine(commands "`nexit")  ; Always exit at the end!
    ; Read and return the output of all commands
    return exec.StdOut.ReadAll()

Executes the given code as a new AutoHotkey process.

ExecScript(Script, Wait:=true)
    shell := ComObject("WScript.Shell")
    exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
    if Wait
        return exec.StdOut.ReadAll()

; Example:
ib := InputBox("Enter an expression to evaluate as a new script.",,, 'Ord("*")')
if ib.result = "Cancel"
result := ExecScript('FileAppend ' ib.value ', "*"')
MsgBox "Result: " result