Is there a way to run Windows command line commands in AutoHotKey without just opening a command prompt and sending the key presses?

I want to run commands hidden in the background. For example

del C:\Users\Test\Desktop\test.txt

I know there is a delete file command in AutoHotKey but I want to be able to run other command line commands that don't work in AHK. That was just an example.


run, %comspec% /c del C:\Users\Test\Desktop\test.txt,,hide

%comspec% points to cmd.exe if you need to run as admin, you'll want to look at runas.


You don't have to use %ComSpec% as pointed by the other answer. You can use cmd.exe and use /c which is the command indicator, it will run anything in front of it, inside cmd.

Run cmd.exe /c del C:\Users\Test\Desktop\test.txt

Here's how the syntax of it looks like:

Run, Target, WorkingDir, Options, OutputVarPID

RunWait, Target, WorkingDir, Options, OutputVarPID

If you need for a command to finish and then continue, use RunWait. To save the output of a command, for example if you want to save your WAN IP address in your clipboard, you can pipe the output with the clip command.

RunWait, cmd.exe /c nslookup myip.opendns.com resolver1.opendns.com | clip

If you need a one line answer, you can use the find command to remove unwanted lines:

RunWait, cmd.exe /c nslookup myip.opendns.com resolver1.opendns.com | find /i "address" | find /v "" | clip

You can hide the cmd window from showing up with the option hide:

Run cmd.exe /c del C:\Users\Test\Desktop\test.txt,,hide


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.