はじめに
Powershellの現在の最新バージョンは、7.0.2です。
しかし、右クリックで.ps1ファイルを実行すると古いバージョンが実行されてしまいます。
そこで、新しいバージョンで動かせるようにレジストリを変更しました。
Pwshに変更する
Pwsh.exeに変更しつつ、"-NoExit"を渡しています。
また、管理者として実行も追加しています。
toPwsh.reg
Windows Registry Editor Version 5.00
; Powershell to Pwsh (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\Command]
@="\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\" \"-NoExit\" \"-Command\" \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'\""
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\runas\Command]
@="\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\" \"-NoExit\" \"-Command\" \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'\""
Powershellに戻す
Powershell.exeに変更しつつ、"-NoExit"を渡さなくしています。
管理者として実行は削除していません。
toPowershell.reg
Windows Registry Editor Version 5.00
; Resotre powhershell
[HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell\command]
@="powershell.exe -noexit -command Set-Location -literalPath '%V'"
[HKEY_CLASSES_ROOT\Directory\shell\Powershell\command]
@="powershell.exe -noexit -command Set-Location -literalPath '%V'"
[HKEY_CLASSES_ROOT\Drive\shell\Powershell\command]
@="powershell.exe -noexit -command Set-Location -literalPath '%V'"
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\DefaultIcon]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe\",1"
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" \"-Command\" \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'\""
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\runas\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" \"-Command\" \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'\""