I have tried Google, but perhaps Google mojo is bad, since I have not found and suitable explanation
I have a batch file that stops and starts some services. This batch file is then scheduled in task scheduler using a service account.
If I change the extension from .bat to .cmd then the script fails, since the user does not appear to have the rights to stop and start a service.
But why does this change behavior just by changing the extension of the script?
41 Answer
What is the difference from using bat and cmd?
BAT was created to interact with COMMAND.COM, the command interpreter of DOS. Microsoft adopted most of the DOS commands into their new interpreter named CMD. EXE. CMD was created to interface with CMD.EXE and it breaks compatibility with COMMAND.COM. Another key difference is in how they handle the errorlevel variable. When using BAT, this variable is only changed once an actual error occurs and no change in state occurs when the each command executes successfully. This is not true for CMD as the errorlevel variable would still change state even if no errors occur. Programmers should take note of this when creating elaborate scripts as it may cause a little bit of confusion.
Aside from those minor differences, CMD and BAT are identical to each other. Most users who create simple scripts to clear or transfer files around should not encounter any problem. For users of the more recent versions of Windows, BAT and CMD are pretty much interchangeable as CMD.EXE would interpret and execute the commands in both files. Although most users are aware of this fact, a lot of the older people who had a chance to work with DOS and its batch files still use the BAT extension; simply out of habit and familiarity.
Summary:
1. The BAT extension is used by DOS and Windows while the CMD extension is for Windows NT Command Scripts
2. The BAT extension can be interpreted by COMMAND.COM and CMD.EXE while the CMD extension can only be interpreted by CMD.EXE
3. The errorlevel always changes state in CMD but only on errors in BAT
If I change the extension from .bat to .cmd then the script fails
In order to fully give you an accurate answer on this portion of your question, the logic you are using needs to be disclosed in order to understand what it's doing to help troubleshoot.
Otherwise, I would assume the failure you are seeing has to do with how the .CMD scripts handle errorlevel differences or perhaps some Group Policy or other restrictions that affects the interpretation by CMD.exe and not when run and interpreted by COMMAND.COM.
Further Resources
- Difference Between CMD and BAT
- WWoIT - Difference between bat and cmd
- StackOverflow - Windows batch files: .bat vs .cmd?
- TechNet - What's the difference between a .cmd and a .bat file?