How `[System.Console]::OutputEncoding/InputEncoding` with Python?
<p>Under Powershell v5, Windows 8.1, Python 3. Why these fails and how to fix?</p>

<pre class="lang-bsh prettyprint-override"><code>[system.console]::InputEncoding = [System.Text.Encoding]::UTF8; 
[system.console]::OutputEncoding = [System.Text.Encoding]::UTF8; 
chcp; 
"import sys
print(sys.stdout.encoding)
print(sys.stdin.encoding)
sys.stdout.write(sys.stdin.readline())
" | 
sc test.py -Encoding utf8; 
[char]0x0422+[char]0x0415+[char]0x0421+[char]0x0422+"`n" | py -3 test.py
</code></pre>

<p>prints:</p>

<pre><code>Active code page: 65001
cp65001
cp1251
Ð¿Â»Ñ????
</code></pre>

<p>Why not embed CPython in powershell?! CPython is so easy to embed, and powershell is very good REPL to <a href="https://msdn.microsoft.com/en-us/powershell/scripting/getting-started/cookbooks/creating-.net-and-com-objects--new-object-" rel="nofollow">play with .NET and COM objects</a>. Here is a simple introduction to using <a href="https://github.com/pythonnet/pythonnet" rel="nofollow">pythonnet</a> from PowerShell. Note how encoding is automatically propagated from powershell to python. </p>

<pre><code>Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. All rights reserved.

PS C:\Users\denfromufa&gt; [system.console]::InputEncoding = [System.Text.Encoding]::UTF8;
PS C:\Users\denfromufa&gt; [system.console]::OutputEncoding = [System.Text.Encoding]::UTF8;
PS C:\Users\denfromufa&gt; [Reflection.Assembly]::LoadFile("C:\Python\Miniconda3_64b\Lib\site-packages\Python.Runtime.dll")


GAC    Version        Location
---    -------        --------
False  v4.0.30319     C:\Python\Miniconda3_64b\Lib\site-packages\Python.Runtime.dll


PS C:\Users\denfromufa&gt; $gil = [Python.Runtime.Py]::GIL()
PS C:\Users\denfromufa&gt; $sys=[Python.Runtime.Py]::Import("sys")
PS C:\Users\denfromufa&gt; $sys.stdin.encoding.ToString()
cp65001
PS C:\Users\denfromufa&gt; $sys.stdout.encoding.ToString()
cp65001
PS C:\Users\denfromufa&gt; $gil.Dispose()
PS C:\Users\denfromufa&gt; [Python.Runtime.PythonEngine]::Shutdown()
PS C:\Users\denfromufa&gt;
</code></pre>

python .net shell powershell