LoginSignup
6
6

More than 1 year has passed since last update.

VSCodeでPowerShell Coreをデフォルトにしたい (マルチ環境編)

Last updated at Posted at 2020-10-02

追記: 2021/07/03

"%USERPROFILE%${env:USERPROFILE}に置き換えると普通にいけました。はい。終わり。

settings.json
{
  "terminal.integrated.profiles.windows": {
    "pwsh": {
      "path": ["${env:USERPROFILE}\\scoop\\apps\\pwsh\\current\\pwsh.exe"],
      "icon": "terminal-powershell"
    },
  },
}

追記 2021/05/08

VSCodeのApril 2021 (version 1.56) アップデートでterminal.integrated.shellterminal.integrated.shellArgs設定が"deprecated"になりました。

以下の説明で上記の属性を使用していますが、代わりに新しく追加されたterminal.integrated.defaultProfile.windowsと、terminal.integrated.profiles.windowsという設定を使用して以下のように設定すると同じ事が出来るようになります。

{
  "terminal.integrated.profiles.windows": {
    "pwsh": {
      // %USERPROFILE% が違ったパスを解釈するのでcmd起動後にPowershell Coreを起動するようにする
      // https://github.com/microsoft/vscode/issues/26048
      "path": [
        "${env:windir}\\Sysnative\\cmd.exe",
        "${env:windir}\\System32\\cmd.exe"
      ],
      "args": ["/c", "%USERPROFILE%\\scoop\\apps\\pwsh\\current\\pwsh.exe"],
      "icon": "terminal-powershell"
    },
  },
  "terminal.integrated.defaultProfile.windows": "pwsh",
}

追記終わり

―――

Windows環境のお話です。

VSCodeのTerminalで、既定のシェルを変更したい場合、コマンドパレット(ctrl + shift + p)を開いて「既定のシェルを選択」からシェルを選択します。普通ならこれだけで終わりです。

ところがScoopからPowerShell Coreを入れた場合は「既定のシェルを選択」の選択肢に出てこないようです。↓

image.png

そういう場合はsettings.jsonにterminal.integrated.shell.windowsという項目を追加し、PowerShell Coreの実行ファイルのパスを記載します。(パスがわからない場合はterminalでwhich pwshと打ちましょう)

settings.json
{
   // other settings..

   "terminal.integrated.shell.windows": "C:\\Users\\username\\scoop\\apps\\powershell-core\\current\\pwsh.exe",

   // other settings..
}

これで無事PowerShellCoreが既定のシェルになりました。
しかし他のPC上のVSCodeと設定を同期している場合、PCのユーザー名は異なる場合が多いと思います。
そうなるとこのパスの記述ではほかのPC上で実行ファイルが開けずエラーになります。

そこで%USERPROFILE%という環境変数を指定してやります。そうするとC:\\Users\\usernameの部分は解決できるわけです。

settings.json
{
   // other settings..

   "terminal.integrated.shell.windows": "`%USERPROFILE%`\\scoop\\apps\\powershell-core\\current\\pwsh.exe",

   // other settings..
}

めでたしめでたし、と思いきや上記の設定を保存してターミナルを開こうとすると以下のエラーが出てきます。

image.png

これは以下のIssueでその原因が説明されています。

I also think that %USERPROFILE% is resolved to the non-existing path C:\WINDOWS\system32\config\systemprofile in VSCode when it is used in the system variables instead of the user variables is relevant to look at. VSCode simply seems to not take user-variables into account which are supposed to overwrite system variables, and instead falls back to the system defaults.

VSCodeは%USERPROFILE%を”C:\WINDOWS\system32\config\systemprofile”に解決してしまうようです。こまりました。

解決策としては、

  • 既定のシェルにはcmdを指定
  • cmdが起動したらPowerShell Coreを起動し、cmdを閉じるようにする

とすることで回避できました。settings.jsonには以下のように記述します。
この設定をして新しいターミナルを開くとまず「1: cmd」が開き、次に「2: pwsh」が開きます。その後すぐさまcmdが閉じるので、「1: pwsh」に変わるはずです。

settings.json
{
   // other settings..

   // 既定のshellはcmdに
   "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
   // pwshを起動させる. "/c" をつけることでコマンド実行後にcmdは終了.
   "terminal.integrated.shellArgs.windows": ["/c", "%USERPROFILE%\\scoop\\apps\\pwsh\\current\\pwsh.exe"], 

   // other settings..
}

上記設定を保存して新しいターミナルを開くと↓のようになります。やったね : )

image.png

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