269

How can I execute a windows command line in the background, without it interacting with the active user?

3
  • 1
    Can you specify what you want to do? Do you want to perform a command on the command line in background or do you want to perform the whole command line in background, so it is unvisible from the desktop? Commented Oct 12, 2010 at 6:20
  • i need two cane perform a command on the command line in background or do you want to perform the whole command line in background Commented Oct 12, 2010 at 6:41
  • Duplicates this question on ServerFault. Commented Aug 16, 2015 at 20:11

12 Answers 12

55

Your question is pretty vague, but there is a post on ServerFault which may contain the information you need. The answer there describes how to run a batch file window hidden:

You could run it silently using a Windows Script file instead. The Run Method allows you running a script in invisible mode. Create a .vbs file like this one

Dim WinScriptHost Set WinScriptHost = CreateObject("WScript.Shell") WinScriptHost.Run Chr(34) & "C:\Scheduled Jobs\mybat.bat" & Chr(34), 0 Set WinScriptHost = Nothing 

and schedule it. The second argument in this example sets the window style. 0 means "hide the window."

4
  • This is perfect. SetPoint for Logitech never, NEVER, starts with windows. I've been starting it manually for about 3 years now. Does it matter where the batch is? I've seen some people put this type of batch file in C, or the root. Commented Jan 31, 2016 at 0:00
  • 4
    It really isn't vague if you are used to linux. Just put a & at the end and its backgrounded. Commented Feb 8, 2021 at 7:14
  • Probably goes without saying, but in Task Scheduler, run the .vbs file using wscript.exe by setting the 'Action' to run wscript (usually %WINDIR%\System32\wcript.exe) and using the fully-qualified path to the .vbs file as an argument. (Other use cases might use cscript - same directory as wscript) Commented Apr 15, 2024 at 10:29
  • To run it from the command line, use wscript MYSCRIPT.vbs Commented Feb 18 at 17:20
386

This is a little late but I just ran across this question while searching for the answer myself and I found this:

START /B program 

which, on Windows, is the closest to the Linux command:

program & 

From the console HELP system:

C:\>HELP START Starts a separate window to run a specified program or command. START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED] [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL] [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B] [command/program] [parameters] "title" Title to display in window title bar. path Starting directory. B Start application without creating a new window. The application has ^C handling ignored. Unless the application enables ^C processing, ^Break is the only way to interrupt the application. 

One problem I saw with it is that you have more than one program writing to the console window, it gets a little confusing and jumbled.

To make it not interact with the user, you can redirect the output to a file:

START /B program > somefile.txt 
12
  • 5
    I like this answer best because it doesn't open another command window Commented Jan 3, 2014 at 16:17
  • 12
    This doesn't seem to work for me, it seems to only create a new cmd instance [?] however if I run it like start /B "" program then it worked... Commented Jun 24, 2015 at 4:56
  • 4
    @rogerdpack That's right. For some reason with Windows 7, this is the command format. The "" is the mandatory title parameter. Commented Jan 30, 2016 at 23:58
  • 24
    Unfortunately, if I exit the shell window in which I spawned the process, it looks like the process also terminates. Commented Jul 21, 2016 at 22:36
  • 2
    @Qwerty: Look at the MSDN for SetConsoleCtrlHandler Commented Sep 12, 2016 at 19:29
101

I suspect you mean: Run something in the background and get the command line back immediately with the launched program continuing.

START "" program 

Which is the Unix equivalent of

program & 
8
  • 10
    what is the fg equivalent? Can we close the command prompt and the porgram will still run? Commented Oct 15, 2014 at 11:34
  • 1
    Also, I want to run a program in command prompt and return to it from time to time, like in screen - is that doable with this? I need to be able to close the command prompt but keep the running program usable. Commented Oct 15, 2014 at 11:36
  • 6
    What's that empty parameter of start? It doesn't work without it (executes just a new command instance), but start's help doesn't say anything about it, it states all parameters are optinional (or I don't understand it). Commented Oct 14, 2015 at 17:41
  • @DawidFerenczy start works without the empty parameter for me, but I seem to get a shells with a separate configuration when I use the empty parameter, as a setting I did when I didn't have the empty parameter isn't used when I do use the empty parameter. I wonder why they use separate configurations? Commented Apr 13, 2016 at 15:23
  • 1
    @Paul START "" program starts a command in a new terminal for me, while program & in Unix runs the command in and prints the output to the same terminal. Commented Apr 13, 2016 at 15:25
19
START /MIN program 

the above one is pretty closer with its Unix counterpart program &

12

You can use this (commented!) PowerShell script:

# Create the .NET objects $psi = New-Object System.Diagnostics.ProcessStartInfo $newproc = New-Object System.Diagnostics.Process # Basic stuff, process name and arguments $psi.FileName = $args[0] $psi.Arguments = $args[1] # Hide any window it might try to create $psi.CreateNoWindow = $true $psi.WindowStyle = 'Hidden' # Set up and start the process $newproc.StartInfo = $psi $newproc.Start() # Return the process object to the caller $newproc 

Save it as a .ps1 file. After enabling script execution (see Enabling Scripts in the PowerShell tag wiki), you can pass it one or two strings: the name of the executable and optionally the arguments line. For example:

.\hideproc.ps1 'sc' 'stop SomeService' 

I confirm that this works on Windows 10.

1
  • 6
    yep start /b no longer works. Commented May 6, 2018 at 5:32
6

This is how my PHP internal server goes into background. So technically it should work for all.

start /B "" php -S 0.0.0.0:8000 & 

Thanks

5

A related answer, with 2 examples:

  1. Below opens calc.exe:

call START /B "my calc" "calc.exe"

  1. Sometimes foreground is not desireable, then you run minimized as below:

call start /min "n" "notepad.exe"

call START /MIN "my mongod" "%ProgramFiles%\MongoDB\Server\3.4\bin\mongod.exe"

Hope that helps.

7
  • 1
    This doesn't seem to run it minimized: call Start /MIN "c" "calc.exe" Commented Mar 12, 2018 at 4:19
  • 1
    correct, it works for notepad: call start /min "n" "notepad.exe" Commented Mar 12, 2018 at 6:44
  • 1
    So it works for windowed applications but not for console applications. Figures, as one can pass the SW_* to CreateProcessW via STARTUPINFO::wShowWindow (including SW_HIDE). Commented Aug 14, 2018 at 18:57
  • What is a full command, with above like "start" or other tool? do we need to write another program? Commented Aug 15, 2018 at 1:50
  • @0xC0000022L No, you just need to pass Process Creation Flag of CREATE_NO_WINDOW, as simple as that. Commented Aug 17, 2022 at 15:16
3

If you want the command-line program to run without the user even knowing about it, define it as a Windows Service and it will run on a schedule.

2
  • 4
    how do you do that? Commented Sep 30, 2011 at 19:26
  • 1
    Alternatively you can make it a scheduled task - Control Panel->Administrative Tools->Scheduled Tasks or use the schtasks command in Windows XP and above (warning: schtasks is complicated). Commented Apr 6, 2012 at 19:02
0

just came across this thread windows 7 , using power shell, runs executable's in the background , exact same as unix filename &

example: start -NoNewWindow filename

help start

NAME Start-Process

SYNTAX Start-Process [-FilePath] [[-ArgumentList] ] [-Credential ] [-WorkingDirectory ] [-LoadUserProfile] [-NoNewWindow] [-PassThru] [-RedirectStandardError ] [-RedirectStandardInput ] [-RedirectStandardOutput ] [-Wait] [-WindowStyle {Normal | Hidden | Minimized | Maximized}] [-UseNewEnvironment] []

Start-Process [-FilePath] <string> [[-ArgumentList] <string[]>] [-WorkingDirectory <string>] [-PassThru] [-Verb <string>] [-Wait] [-WindowStyle <ProcessWindowStyle> {Normal | Hidden | Minimized | Maximized}] [<CommonParameters>] 

ALIASES saps start

1
  • I don't have Windows 7 anymore to test this on [Windows 11 here] -- but start.exe is also a command. To make this work, without aliases since 'start' is a different but similar command, call it directly from within PowerShell: Start-Process -NoNewWindow filename Commented Nov 18, 2022 at 13:49
0

You can see the correct way to do this in this link:

How to Run a Scheduled Task Without a Command Window Appearing

Summarizing, you have to checkbox for 'Run whether user is logged on or not'. Task user credentials should be enter after pressing 'Ok'.

0

You can use my utility. I think the source code should be self explanatory. Basically CreateProcess with CREATE_NO_WINDOW flag.

-1

I did this in a batch file: by starting the apps and sending them to the background. Not exact to the spec, but it worked and I could see them start.

rem Work Start Batch Job from Desktop rem Launchs All Work Apps @echo off start "Start OneDrive" "C:\Users\username\AppData\Local\Microsoft\OneDrive\OneDrive.exe" start "Start Google Sync" "C:\Program Files\Google\Drive\GoogleDriveSync.exe" start skype start "Start Teams" "C:\Users\username\AppData\Local\Microsoft\Teams\current\Teams.exe" start Slack start Zoom sleep 10 taskkill /IM "explorer.exe" taskkill /IM "teams.exe" taskkill /IM "skype.exe" taskkill /IM "slack.exe" taskkill /IM "zoom.exe" taskkill /IM "cmd.exe" @echo on 

killing explorer kills all explorer windows, I run this batch file after start up, so killing explorer is no issue for me. You can seemingly have multiple explorer processes and kill them individually but I could not get it to work. killing cmd.exe is to close the CMD window which starts because of the bad apps erroring.

2
  • 2
    You are launching work apps and then terminating them all ? Why ? Commented Apr 23, 2021 at 22:10
  • 1
    Amit, many of the comments in previous questions asked about how to interact/close running programs you open. This is apparently an illustration of how to do that. Commented Nov 18, 2022 at 13:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.