This blog is about Java (advanced Java topics like Reflection, Byte Code transformation, Code Generation), Maven, Web technologies, Raspberry Pi and IT in general.

Sonntag, 17. Januar 2016

How to set the encoding of a Powershell console window

I created some Unix like Java console programs. One program reads the stdin, writes to the stdout and then the next program reads the output and processes it. This worked very well with Cygwin or a Unix shell. The whole thing looks like this:

> java -jar prog1.jar | java -jar prog2.jar

 The Java code is quite simple a well:

Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
  String line = scanner.nextLine();
  System.out.println(doSomething(line));
}

My problem was that this example broke special characters if I did the same thing in the Windows Powershell. The reason is that a Powershell console window doesn't use the default system encoding, but some strange encoding. In my case it's IBM850. However my default system encoding is window-1252. My Java programs expected windows-1252 and therefore the encoding of special chars was broken. Why the hell uses a Powershell window IBM850? I have no idea. But I cost my several hours to figure it out and to figure out how to change the encoding. To change the encoding to the default system encoding you need to run this three commands:

$OutputEncoding = [system.text.encoding]::GetEncoding([System.Text.Encoding]::Default.CodePage)
[console]::InputEncoding = [system.text.encoding]::GetEncoding([System.Text.Encoding]::Default.CodePage)
[console]::OutputEncoding = [system.text.encoding]::GetEncoding([System.Text.Encoding]::Default.CodePage)

Again i have no idea why you need to set the encoding three times. After this point I just did it and I was happy that it worked.

How to get the boot time of a Windows 7 or Windows 10 system with Powershell

I must admit that i have a quite bad memory. Most of the time I can't remember when I arrived at work. But there is a quite easy way to figure it out, if the first thing you do is to turn on the computer. With this little Powershell command you see the time of the first system event. Because if you turn on the computer and login will generate system events this method is quite accurate.

> Get-EventLog System -After (Get-Date -F 'yyyy-MM-dd') | select -Last 1 | % {$_.TimeGenerated.ToString("HH:mm")}