NOTE: Open Administrative PowerShell (not pwsh.exe) and run the every steps below.
Firstly check the actual component name:
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Then, install the OpenSSH Server (in the case, OpenSSH.Server~~~~0.0.1.0):
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Set-Service -Name sshd -StartupType 'Automatic'
Start-Service sshd
if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
}
# Your client's public key
$authorizedKey="ssh-ed25519 AAAA...."
powershell Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value '''$authorizedKey'''
icacls.exe "$env:ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Users\kawasaki\AppData\Local\Microsoft\WindowsApps\pwsh.exe" -PropertyType String -Force
https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement