For general scripting/automating on Windows, I generally prefer PowerShell. There are times, however, when “batch scripting” with CMD.EXE is needed. If you’ve ever tried writing scripts of any complexity, you know that “batch scripts” can be a real pain to deal with. In particular, expansion of variables happens by default when a command is read, which is not what many other scripting languages do. Raymond Chen has a nice post describing this in further detail.
For this reason, it can be useful to turn on delayed expansion if you’re doing anything even remotely interesting. One of the unexpected downsides — or bugs, in fact — of delayed variable expansion is that the ‘!’ character seems to eaten… so if you have a command you’re calling that uses a ‘!’ in its syntax, you’re plain out of luck. The good news is that you can use the delayed-expansion behavior to... [More]