0

I'm using this site for a some time without ever finding the need to ask a question myself as most of the time I find the answer fairly easily

But my next question is kinda hard to search for with keywords, so here I am asking.

I am trying to take an output of an array, and redirect it to a batch file. For example:

PS C:\Users\Administrator> $Exmpl = "echo one","echo two"
PS C:\Users\Administrator> $Exmpl
echo one
echo two
PS C:\Users\Administrator> $Exmpl > ExmplFile.bat

So far so good. The problem begins when I want to execute this new script (In this example I'm using the same shell but it works the same in a CMD shell).

PS C:\Users\Administrator> .\ExmplFile.bat
C:\Users\Administrator>■e
'■e' is not recognized as an internal or external command, operable program or batch file.

After a little exploring, I found:

  1. It acts the same even if I create the file with no extentions and later on add the .bat extension.
  2. If I open the file, and change the entire content to something else like "cd .." - the same error occurs. Like the whole file is damaged from its' creation.
  3. The "e" in the string in the error ■e refers to the first letter in the file. For example after I've changed the content to the command "cd .." - the string in the execution error was ■c. Like it detects an unknown character before the first letter, and after the first letter it detects some sort of a line break.

Can you guys please share your knowledge as I assume it's not a hard question for those of you who know how the redirection to a file in powershell works? Thanks in advance.

YuvalS
  • 5
  • 1

1 Answers1

0

Sounds like an encoding problem. Output redirection is probably using multibyte characters, e.g., UTF-16 or what .Net calls "Unicode". Alternately, it's UTF-8 with a byte order mark.

Try:

Set-Content -Path 'ExmplFile.bat' -Value $Exmpl -Encoding Ascii

Or:

$Exmpl | Out-File -FilePath 'ExmplFile.bat' -Encoding ascii
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • Thank you very much, you've helped me a lot with this answer not only because this question was holding me down, but also because I am pretty ignorant about encoding. – YuvalS Aug 06 '16 at 23:24
  • Well, this phrase: "Output redirection is probably using multibyte characters" should be read as "The _default_ output encoding in PowerShell is NOT Ascii (as it should be) and this point frequently cause problems to new PowerShell users"! There is another example of this problem [here](http://stackoverflow.com/questions/35313953/findstr-always-fails-to-find-hash-string-in-text-file/), and there are _a lot_ of similar cases. – Aacini Aug 06 '16 at 23:48
  • @Aacini Actually, it depends. If the item on the left of the `>` is a program, then it's ANSI/ASCII. If it's a PowerShell cmdlet or .Net function, it's Unicode. I never remember what `>` defaults to, or `Out-File` or `Set-Content`/`Add-Content`. I just remember that all three are different, and that `Out-File`'s parameters don't match `Set-Content`/`Add-Content`'s parameters. I know *why* that's the case so I don't need that explained, but it's still arbitrary, inconsistent, and not memorable. Therefore, it's irrelevant. Avoid `>` and always specify encoding elsewhere. It's the only sane way. – Bacon Bits Aug 07 '16 at 00:25
  • @BaconBits, if a program or a .net function outputs the data, the format is not defined by PowerShell but by the program/function itself... – aschipfl Aug 07 '16 at 10:52