はじめに
Windows Terminalを入れ、PowerShellも7にしてみました。
しかし、右クリックメニューからスクリプトファイルを実行する場合、従来のターミナル経由で古いPowerShellが実行されてしまいます。
これを右クリックメニューで、
- ターミナル: Windows Terminal
- シェル: PowerShellCore
で実行できるようにしたいと思います。
ターミナルをWindows Terminalにすると、出力結果を検索できたり、メッセージを選択してもスクリプトの動作を停止しなくなったりと便利になりますので多くの人に試してもらいたいと思っています。
環境
Window TerminalとPowerShellCoreともにパスが通っているとします。
バージョン: 1.7.1033.0
Name Value
---- -----
PSVersion 7.1.3
PSEdition Core
GitCommitId 7.1.3
OS Microsoft Windows 10.0.19042
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
コマンドで、Window Terminal & PowerShellCore でスクリプトファイルを実行する
まずは、基礎知識としてターミナルにコマンドを打ってWindow Terminal & PowerShellCoreを実行する場合は、
PS >wt pwsh -noexit "script.ps1"
などとします。
-noexit
は、必須ではなくこれを渡すとスクリプトの実行が終わっても閉じないようになります。
エクスプローラーから、Window Terminal & PowerShellCore でスクリプトファイルを実行する
右クリックメニューから実行できるようにする".reg"ファイルは、以下のようになります。
これを結合すると
- ディレクトリを選択してから右クリックすると出る"PowerShellで開く"メニューを削除する
- スクリプトファイルのアイコンを PowerShellCore に置き換える
- スクリプトファイルを Window Terminal & PowerShellCore を用いて実行する右クリックメニューを追加する
- スクリプトファイルを管理者権限で Window Terminal & PowerShellCore を用いて実行する右クリックメニューを追加する
となります。
このファイルでは、wt
とpwsh
のようにパスが通っている前提で登録しています。
確実を期すためには、絶対パスで登録したほうが無難です。
それぞれの絶対パスは、以下になります。
- "%LocalAppData%\Microsoft\WindowsApps\wt.exe"
- "%ProgramFiles%\PowerShell\PowerShell\7\pwsh.exe"
Windows Registry Editor Version 5.00
; Powershell to Pwsh (WindowTerminal & Powershell7)
[-HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell]
[-HKEY_CLASSES_ROOT\Directory\shell\Powershell]
[-HKEY_CLASSES_ROOT\Drive\shell\Powershell]
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\DefaultIcon]
@="\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\",-32512"
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0]
@="PowerShellで実行"
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command]
@="wt new-tab --title \"PowerShell\" pwsh -NoExit -Command \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }\\; & '%1'\""
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\runas\Command]
@="wt new-tab --title \"PowerShell\" pwsh" -NoExit -Command \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }\\; & '%1'\""