Simple batch file fails on Windows Server 2012

22,542

Solution 1

It looks like the redirect > has encoded your batch file in unicode, when it should be ascii. In Powershell, use Out-File instead of the redirect like this:

"echo test" | Out-File test.bat -Encoding ascii

Running the batch file after the following encoding will reproduce the error:

"echo test" | Out-File test.bat -Encoding unicode

Solution 2

Had the same problem, thought I was crazy.

Make your batch file on another computer. I had the same issue opening Notepad and typing the commands normally. It always gave me the same message. Once I recreated the batch file (do not copy the old file) on my local Win7 device, copied it back to my Win 2012R2 server, worked like a champ.

Share:
22,542

Related videos on Youtube

user340089
Author by

user340089

Updated on September 18, 2022

Comments

  • user340089
    user340089 over 1 year

    On a Windows 2012 Server, from a logged-in Administrator (local and domain), created a batch file "test.bat" by redirecting console input into a file:

    echo Hello
    

    When run from PowerShell cmd prompt as .\test.bat, it echoes:

    '■e' is not recognized as an internal or external command,
    

    Batch file is in logged-in user's C:\users\%username% directory.

    The file was created from console, thus:

    echo "echo Hello" > test.bat
    

    and then edited with Notepad. Looked alright in Notepad... nothing unusual.

    Any ideas as to what's going on?

    EDITED:

    Per suggestion, I modified command line input to remove quotes. Therefore, entered the command like this: echo echo Yowzer > test.bat Ran as: .\test.bat and got the same response as above. Also note that if I edit in Notepad the above test.bat (i.e. created at cmd line) and re-run, it still returns a garbage response. However, if I create/save the file in Notepad from the start, it works correctly.

    Can anyone duplicate this?

    • Logman
      Logman almost 10 years
      "C:\users\%username%"?? not in subdir like documents? did you cd into the folder that contains the bat file and then run ".\test bat"? And you are executing this from a poweshell console right?
    • user340089
      user340089 almost 10 years
      The bat file was both created and run from C:\subdir. Two methods were used to create the bat file: (1) echo stuff > bat.bat from keyboard, and (2) write in Notepad++ and save as bat file. All cmd prompt activity was done from a PoSH console.
    • barlop
      barlop over 8 years
      -1 for abandoning the question, thus decreasing its value
  • user340089
    user340089 almost 10 years
    That did not fix the problem. Inputted: echo echo Yowzer > test.bat Ran as: .\test.bat and still got the same response as above. Note that if I edit the file in Notepad, it still returns a garbage response. However, if I create/save the file in Notepad from the start, all is well. Can anyone duplicate this?
  • Keltari
    Keltari almost 10 years
    strange. it worked for me.
  • user340089
    user340089 almost 10 years
    Were you able to replicate the problem I was having? That is, did you see the weird result reported above when using quote marks?
  • Keltari
    Keltari almost 10 years
    yes. I removed the quotes and it worked as intended
  • barlop
    barlop over 8 years
    -1 He said "were you able to replicate the problem I was having" And the problem he was having was was those funny characters. And you replied saying "yes...". And it's clear from his question and even more clear from his reply, that what you speak of "the quotes break the string", is absolutely and obviously not the problem
  • boot13
    boot13 over 5 years
    I have no idea why, but this worked for me in a similar situation.