最近在處理 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 `
-Force
Restart-Service sshd
運行完,可用 Get-ItemProperty HKLM:\SOFTWARE\OpenSSH | Select-Object DefaultShell 查看預設的情況。
連續動作:
```
Windows PowerShell
著作權(C) Microsoft Corporation。保留擁有權利。
安裝最新的 PowerShell 以取得新功能和改進功能!https://aka.ms/PSWindows
PS C:\WINDOWS\system32> New-ItemProperty `
>> -Path "HKLM:\SOFTWARE\OpenSSH" `
>> -Name DefaultShell `
>> -Value "C:\Windows\System32\cmd.exe" `
>> -PropertyType String `
>> -Force
DefaultShell : C:\Windows\System32\cmd.exe
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
PSChildName : OpenSSH
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> Restart-Service sshd
PS C:\WINDOWS\system32> Get-ItemProperty HKLM:\SOFTWARE\OpenSSH | Select-Object DefaultShell
DefaultShell
------------
C:\Windows\System32\cmd.exe
```
winget install --id Microsoft.PowerShell --installer-type wix
這時安裝完路徑會顯示在 `C:\Program Files\PowerShell\7\pwsh.exe` ,這版就可以當 ssh shell 來用了:
```
PS C:\WINDOWS\system32> Get-Command pwsh
CommandType Name Version Source
----------- ---- ------- ------
Application pwsh.exe 7.6.1.0 C:\Program Files\PowerShell\7\pwsh.exe
PS C:\WINDOWS\system32> New-ItemProperty `
>> -Path "HKLM:\SOFTWARE\OpenSSH" `
>> -Name DefaultShell `
>> -Value "C:\Program Files\PowerShell\7\pwsh.exe" `
>> -PropertyType String `
>> -Force
DefaultShell : C:\Program Files\PowerShell\7\pwsh.exe
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
PSChildName : OpenSSH
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> Restart-Service sshd
```
如此遠端到 Windows 11 時,登入後會看到版本資訊:
```
PowerShell 7.6.1
PS C:\Users\user>
```