0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows+CursorでPowerShellの文字化けを一掃する手順(UTF‑8化・PowerShell 7・設定まとめ)

Posted at

対象: Windows 10/11 + Cursor(VSCode系)を使っていて、PowerShell実行時に日本語が文字化けする人向け。

ゴール: Cursor の統合ターミナルで PowerShell 7(UTF‑8) を既定で起動し、出力・ファイル入出力・外部コマンドの日本語が崩れない状態にする。


TL;DR(最短手順)

  1. PowerShell 7 を入れる
    winget install --id Microsoft.PowerShell -e
    
  2. UTF‑8 に固定(PowerShell 7 側の $PROFILE に記述):
    notepad $PROFILE
    
    開いたら以下を追記して保存:
    [Console]::InputEncoding  = [System.Text.Encoding]::UTF8
    [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
    $OutputEncoding = [System.Text.Encoding]::UTF8
    
  3. Cursor の既定シェルを PowerShell 7 に変更
    • 設定 → Terminal › Integrated › Default Profile: WindowsPowerShell(= PowerShell 7)を選択。
    • 出ない場合は settings.json に以下を追記:
      {
        "terminal.integrated.defaultProfile.windows": "PowerShell7",
        "terminal.integrated.profiles.windows": {
          "PowerShell7": {
            "path": "C\\\\Program Files\\\\PowerShell\\\\7\\\\pwsh.exe",
            "icon": "terminal-powershell"
          }
        }
      }
      
  4. 確認
    $PSVersionTable.PSVersion    # 7.x.x ならOK
    [Console]::OutputEncoding    # utf-8 ならOK
    

1. なぜ文字化けするの?(背景)

  • Windows には Windows PowerShell 5.1(旧版)と PowerShell 7(現行)が共存します。
  • 旧版 5.1 は既定が Shift_JIS(CP932)、現行 7 は UTF‑8 を前提としています。
  • Cursor/VSCode の既定シェルが 5.1 のままだと、日本語が化けやすいです。

自分がどっちを使っているか確認

$PSHOME
# 例:
#   C:\Windows\System32\WindowsPowerShell\v1.0 → 5.1
#   C:\Program Files\PowerShell\7 → 7.x

2. PowerShell 7 を winget でインストール

winget install --id Microsoft.PowerShell -e
  • 標準のパス:C:\Program Files\PowerShell\7\pwsh.exe
  • インストール後の確認:
    where pwsh               # 見つかれば PATH 済み
    "C:\Program Files\PowerShell\7\pwsh.exe"  # 直接起動も可
    $PSVersionTable.PSVersion
    

PATH に出ない場合where pwsh が空):

  • 環境変数 PathC:\Program Files\PowerShell\7\ を追加 → 再起動
  • もしくは Cursor 側で pwsh.exe の絶対パスを指定(後述)

3. PowerShell 7 を UTF‑8 に“確実に”固定

3.1 $PROFILE に設定(推奨・一番確実)

notepad $PROFILE

以下を追記して保存

[Console]::InputEncoding  = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

$PROFILE の場所例:C:\Users\<User>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

3.2 powershell.config.json で全体設定(7.3+の補助手段)

7.3 以降で有効。環境全体で UTF‑8 にしたいときの補助として。

  • パス:C:\Program Files\PowerShell\7\powershell.config.json
  • 例:
{
  "consoleInputEncoding": "utf-8",
  "consoleOutputEncoding": "utf-8"
}

変更後は 新しい pwsh プロセス を起動し直す。


4. Cursor の既定ターミナルを PowerShell 7 に変更

4.1 GUI で切り替え

  • 設定 → Terminal › Integrated › Default Profile: WindowsPowerShell を選択。
    • PowerShellPowerShell 7Windows PowerShell5.1 です。

4.2 settings.json で明示的に固定(出てこない場合)

{
  "terminal.integrated.defaultProfile.windows": "PowerShell7",
  "terminal.integrated.profiles.windows": {
    "PowerShell7": {
      "path": "C\\Program Files\\PowerShell\\7\\pwsh.exe",
      "icon": "terminal-powershell"
    }
  }
}

保存 → ターミナルを閉じて再度開く。


5. 動作確認(これだけ見ればOK)

$PSVersionTable.PSVersion
[Console]::OutputEncoding

# ファイル入出力の確認
"あいうえお" | Out-File .\utf8-test.txt -Encoding UTF8
Get-Content .\utf8-test.txt -Encoding UTF8
  • 文字が崩れなければ成功。

6. よくあるハマりどころと解決策

  • Cursor で 5.1 が開く
    • Default Profile を PowerShell に直す / settings.json で固定。
    • $PSHOME を確認して 7 のパスになっているかチェック。
  • where pwsh が空
    • C:\Program Files\PowerShell\7\pwsh.exe を直接起動、または PATH に追加。
  • UTF‑8 にならない
    • $PROFILE に 3 行を追記したか確認。新しいターミナルで再確認。
    • 依然ダメなら powershell.config.json を併用し、完全に再起動。
  • 5.1 をどうしても使う必要がある
    • セッション限定で:
      chcp 65001
      $OutputEncoding = [Console]::OutputEncoding = [Text.Encoding]::UTF8
      
    • ただし根本解決は PowerShell 7 への移行

7. 仕上げのチェックリスト

  • pwsh(7.x)が起動する
  • Cursor の Terminal Default Profile が PowerShell(7)
  • [Console]::OutputEncodingutf-8 / CodePage 65001
  • ファイル入出力で日本語が崩れない

付録:コマンド早見表

# バージョン・場所
$PSVersionTable.PSVersion
$PSHOME
where pwsh

# プロファイルを開く(なければ作る)
notepad $PROFILE

# 一時的にUTF-8(5.1などの回避策)
chcp 65001
$OutputEncoding = [Console]::OutputEncoding = [Text.Encoding]::UTF8

以上。これで Cursor × PowerShell でも日本語の文字化けに悩まされません。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?