Title: How to Detect 'Batch' mode from within a Batch File under Windows
Tags: windows;batch-file;cmd
Question: I'm running a batch (.bat) file via the Windows (Task) Scheduler, a third party cron (nncronLite) and via a "START" command.  I'm trying to work out a way that I can detect the .bat file has been run via one of the above means compared to me running the batch file from a console (cmd.exe) window.

I can try checking an environment variable.. but which one (here I'm trying HOMEDRIVE)?

```setlocal
set BATCH_MODE=1
if defined HOMEDRIVE set BATCH_MODE=

rem [...]

if defined BATCH_MODE echo IN BATCH MODE
```

Any other suggested mechanims (other than supplying a command line argument to the batch file = kludge, IMO)?

This is for use with Windows XP, Win7, Win8, Win10 (and beyond, I guess)

Thanks.

Comment: @Aacini: That variable shows the actual command line used to invoke the .bat file when in 'batch' mode and just the name (no .bat) of the batch file when run in a console (cmd.exe) window.  So, maybe I can do something with comparing COMSPEC with CMDLINE or CMDCMDLINE.

BTW, where did you find out about the existence of CMDCMDLINE?  It's not shown by a 'SET' command, nor is it included in the System Environment Variables...?

Comment: Type `set` to list all variables, including `CMDCMDLINE`. Type `set /?` to find the help for the `set` command, which includes the predefined environment variables.

Comment: @Aacini, then it's probably `cmd /?`...

Comment: Ah yes, @Aacini, seems I misunderstood you. You are perfectly right, `CMDCMDLINE` is also a dynamic variable of course; sorry for confusion!

Comment: Look for _the value_ of `%CMDCMDLINE%` variable...

Comment: @aschipfl: `set` command does _not_ list the dynamic variables...

Comment: @aschipfl: what I mean is that `set /?` _describes_ all the dynamic variables, and `set` command list the variables defined in the environment. Because its nature, the _dynamic_ variables are _not_ defined in the environment, so `set` command does _not_ list anyone of they, like CMDCMDLINE, ERRORLEVEL, TIME, RANDOM, etc.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:04/01/17', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<windows><batch-file><cmd>', 'Title': "How to Detect 'Batch' mode from within a Batch File under Windows", 'ViewCount': 189, 'id': 43152442, 'user_AboutMe': '', 'user_AccountId': 4367510.0, 'user_DisplayName': 'Skeeve', 'user_Reputation': 161.0, 'user_Views': 60.0, 'user_id': 7207260.0}}