Skip to content

Instantly share code, notes, and snippets.

@sheepla
Last active September 4, 2023 12:39
Show Gist options
  • Save sheepla/77427fdbdd2716c383524d771cbb95c5 to your computer and use it in GitHub Desktop.
Save sheepla/77427fdbdd2716c383524d771cbb95c5 to your computer and use it in GitHub Desktop.
Install OpenSSH server for Windows and configure to manage Windows computer remotely
<#
.DESCRIPTION
Install OpenSSH and configure to manage Windows computer remotely.
.LINK
https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_overview
#>
Write-Host "Installing OpenSSH..."
Get-WindowsCapability -Online | Where-Object {$_.Name -ILike "openssh*"} | ForEach-Object {
Write-Host " Adding Windows capability: $($_.Name)"
Add-WindowsCapability -Online -Name $_.Name
}
Write-Host "Starting sshd service..."
Start-Service sshd
Write-Host "Configureing to start sshd service automatically..."
Set-Service -Name sshd -StartupType "Automatic"
Write-Host "Checking firewall rule..."
$firewallRule = Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" |
Select-Object -Property @("Name", "Enabled")
if ($null -ne $firewallRule)
{
Write-Output $firewallRule
}
Write-Host "Finished to install OpenSSH! Please connect this computer with the following command:"
Write-Host " ssh ${env:UserName}@${env:ComputerName}"
Write-Host "Configuration file is here:$([IO.Path]::Combine($env:ProgramData, "ssh", "sshd_config"))"
Write-Host "Configureing default shell..."
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force