You should setup your emulators with the proper exit key(s)/button(s) and not use the AM setting to exit. RetroArch's default key to exit is "Esc" but you're sending it to AM instead of RetroArch by having this set.
Are you using RetroPie as your base build? If so, run EmulationStation and setup your keyboard and controls there. It will create the proper exit keys for the emulators when you run them in Attract-Mode. Once you have that setup there is no need for any cmd, taskkill, etc. There is also no need to use the AM setting to exit emulators.