LoginSignup
25
25

More than 3 years have passed since last update.

Powershell上で、パイプで渡すと文字化けする。

Last updated at Posted at 2016-02-11

Powershell上で、コマンドの出力結果をそのままパイプで受け渡しすると、文字化けします。

例えば、次のようなコマンドを打った時、

PS C:\ > ipconfig | clip

ipconfigの出力結果がクリップボードに出力できますが、日本語の環境では文字化けした結果が出力されます。

これはパイプでデータを渡す際に、$OutputEncoding変数の値に従って、文字コードの変換を行ってからデータを渡す動作を行っており、文字コードの変換の過程で文字化けが発生します。
デフォルトの設定がUS-ASCIIのため、結果として日本語は自動的に文字化けします。

以下の様に指定することで、日本語のテキストを文字化けせずに出力できます。

PS C:\ > $OutputEncoding = [System.Text.Encoding]::GetEncoding('shift_jis')

※注意 $OutputEncodingの値の変更による、他への影響が考えられるため、試す際はご注意下さい。

追記:下のコマンドでも大丈夫みたいです。

PS C:\ > $OutputEncoding = [Text.Encoding]::Default
25
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
25