powershellからファイルへのリダイレクションの文字コード
powershell "echo 日本語">a.txt
→ shift_jis
powershell "echo 日本語 >a.txt"
→ unicode
前者は、[console]::OutputEncodingに依存し、デフォルトはshift_jisです。
後者は、unicode固定です。
前者は変更できる文字コードに制限あり。また、元に戻さないと変更が永続するので注意が必要です。
powershell "$encoding=[console]::OutputEncoding;[console]::OutputEncoding=[text.encoding]::getencoding('utf-8');echo 日本語;[console]::OutputEncoding=$encoding;">a.txt
後者は、
> a.txt
を
| out-file -encoding default a.txt
| set-content -encoding string a.txt
などで代替します。
powershell "echo 日本語 | out-file -encoding default a.txt"
powershell "echo 日本語 | set-content -encoding string a.txt"