Bat files won’t run and instantly close!

Hold onto your butts.

I ran into this issue this past weekend while doing some planned work. I wrote some very simple bat files that had a lot of repetitive tasks in them that I simply did not want to manually type out. Without going into more detail, I used PowerShell to dynamically create the bat files. Yes, I know, this sounds dumb but at the time this was the simplest way to tackle this problem.

When it was time to run these I proudly double-clicked the bat files thinking “gee whiz I sure am efficient” and of course nothing happened. Well not exactly. A window would briefly pop open on the screen and immediately close. What’s going on? Just to make sure I wasn’t crazy, I edited one of the files, saved it, and ran it.

echo %username% pause

Same problem. I made sure I was running with proper credentials as well, but no luck. So I tried running the bat file via the command line and saw it report an error:

'■e' is not recognized as an internal or external command, operable program or batch file.

This threw me for a loop. What’s this mystery character it’s not recognizing? If I run the same command for echo %username% in the command line it works fine so it’s not that. I reviewed the file in Notepad++ and was sure to enable the “Show all Characters” option under the view menu just to make sure there weren’t any weird characters. I didn’t see anything here either.

So what fixed it?

As it turns out, my PowerShell script defaulted to save the files as Unicode. I needed to include the -Encoding ascii parameter when I used set-content.

Notepad was defaulting to Unicode as well instead of ANSI for the encoding type since the original file was in the other format. When you go to “Save As” you’ll see a drop down menu next to the Save button that says Encoding. Change this to ANSI. This got everything working for me. Such a simple thing to overlook. Hopefully someone finds this helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *