最近在處理 AI 自動化工作,開發了一些 claude skills ,發現最後需要在 Windows 11 運行 Claude Code CLI ,發現不同筆電的環境容易有問題,研究一陣子後,就決定統一都用 PowerShell 7 吧,也可以把 Windows 11 內建的 OpenSSH server 的 shell 也更換過去。
需要先理解 Windows 11 內建有 Powershell 5 ,但他不能拿來當 ssh 登入使用。這是預設採用 cmd 環境,改壞無法 ssh 登入 Windows 11 可以用這段還原,這是在 Powershell 運行:
New-ItemProperty `-Path "HKLM:\SOFTWARE\OpenSSH" `-Name DefaultShell `-Value "C:\Windows\System32\cmd.exe" `-PropertyType String `-ForceRestart-Service sshd
運行完,可用 Get-ItemProperty HKLM:\SOFTWARE\OpenSSH | Select-Object DefaultShell 查看預設的情況。
連續動作:
```Windows PowerShell著作權(C) Microsoft Corporation。保留擁有權利。安裝最新的 PowerShell 以取得新功能和改進功能!https://aka.ms/PSWindowsPS C:\WINDOWS\system32> New-ItemProperty `>> -Path "HKLM:\SOFTWARE\OpenSSH" `>> -Name DefaultShell `>> -Value "C:\Windows\System32\cmd.exe" `>> -PropertyType String `>> -ForceDefaultShell : C:\Windows\System32\cmd.exePSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSHPSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWAREPSChildName : OpenSSHPSDrive : HKLMPSProvider : Microsoft.PowerShell.Core\RegistryPS C:\WINDOWS\system32>PS C:\WINDOWS\system32> Restart-Service sshdPS C:\WINDOWS\system32> Get-ItemProperty HKLM:\SOFTWARE\OpenSSH | Select-Object DefaultShellDefaultShell------------C:\Windows\System32\cmd.exe```
後續安裝完 Powershell 7 且更正確是要安裝 PowerShell 7 MSI 套件,直接在 MS Store 安裝會是另一個:
winget install --id Microsoft.PowerShell --installer-type wix
這時安裝完路徑會顯示在 `C:\Program Files\PowerShell\7\pwsh.exe` ,這版就可以當 ssh shell 來用了:
```PS C:\WINDOWS\system32> Get-Command pwshCommandType Name Version Source----------- ---- ------- ------Application pwsh.exe 7.6.1.0 C:\Program Files\PowerShell\7\pwsh.exePS C:\WINDOWS\system32> New-ItemProperty `>> -Path "HKLM:\SOFTWARE\OpenSSH" `>> -Name DefaultShell `>> -Value "C:\Program Files\PowerShell\7\pwsh.exe" `>> -PropertyType String `>> -ForceDefaultShell : C:\Program Files\PowerShell\7\pwsh.exePSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSHPSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWAREPSChildName : OpenSSHPSDrive : HKLMPSProvider : Microsoft.PowerShell.Core\RegistryPS C:\WINDOWS\system32>PS C:\WINDOWS\system32> Restart-Service sshd```
如此遠端到 Windows 11 時,登入後會看到版本資訊:
```PowerShell 7.6.1PS C:\Users\user>```