0

I am trying to redirect input in Powershell by

Get-Content input.txt | my-program args

The problem is the piped text is preceded with a BOM (0xefbbbf), and my program cannot handle that correctly. Is there any way to redirect the input without generating a BOM, or is there any workaround?

user
  • 111
  • 1
  • Have you visited this page ? https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content – vssher Feb 08 '20 at 09:29
  • @vssher Yes, and that does not help. There are many options specifying how to read the file, but no one to control how to pass it to the pipeline. – user Feb 08 '20 at 09:34
  • sometimes it helps to know which Powershell you're using, and the program accepting the lines. Sometimes you can direct information to a program using _args_ first the to the program using __>__. – vssher Feb 08 '20 at 09:46
  • This page may help more than the last I pasted up "Write PowerShell Functions That Accept Pipelined Input" it's from 2010 but not to much has changed in the way it's done https://devblogs.microsoft.com/scripting/write-powershell-functions-that-accept-pipelined-input/ – vssher Feb 08 '20 at 09:50
  • @vssher I am using Powershell 5.1, and my program is a java program that reads stdin. I am sure that the only way to pass the data is (possibly redirected) stdin. – user Feb 08 '20 at 10:09
  • As I also commented on [Stack Overflow](https://stackoverflow.com/questions/60124466/how-to-redirect-input-in-powershell-without-bom) where you posted the same question. The utility program you wrote would be much more versatile if you have it check for input prefixed with a Byte Order Mark and have it skip these bytes. As it appears, this utility now completely depends on the fact that all input should be UTF8 without BOM.. – Theo Feb 08 '20 at 12:43

1 Answers1

0

This worked for me:

[Console]::InputEncoding = [System.Text.UTF8Encoding]::new()
Zombo
  • 1
  • 24
  • 120
  • 163