| 1 | <# |
| 2 | .SYNOPSIS |
| 3 | Installs and configures OpenSSH Client and Server on Windows 10/11. |
| 4 | |
| 5 | .DESCRIPTION |
| 6 | Automates the installation of OpenSSH Client and Server Windows Capabilities, configures |
| 7 | the OpenSSH SSHD service to start automatically, starts the service, and verifies/creates |
| 8 | the necessary inbound firewall rule for Port 22. |
| 9 | |
| 10 | .EXAMPLE |
| 11 | irm https://gist.githubusercontent.com/raw/YOUR_GIST_ID/install-openssh.ps1 | iex |
| 12 | Downloads and executes the OpenSSH setup remotely via an elevated PowerShell session. |
| 13 | |
| 14 | .EXAMPLE |
| 15 | .\install-openssh.ps1 |
| 16 | Executes the script locally from an elevated PowerShell session. |
| 17 | |
| 18 | .NOTES |
| 19 | Prerequisites: |
| 20 | - Must run in an Elevated PowerShell Session (Run as Administrator). |
| 21 | - Requires Windows 10 (1809+) or Windows 11 with internet or WSUS access for Windows Capabilities. |
| 22 | |
| 23 | Key Features: |
| 24 | - Idempotent Execution: Safely skips installation/configuration steps if components are already present. |
| 25 | - Automated Service Setup: Configures `sshd` startup type to 'Automatic' and ensures it is running. |
| 26 | - Firewall Provisioning: Verifies or generates the 'OpenSSH-Server-In-TCP' inbound firewall rule. |
| 27 | |
| 28 | .LINK |
| 29 | https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse |
| 30 | #> |
| 31 | |
| 32 | # Requires Elevated PowerShell Session |
| 33 | if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { |
| 34 | throw "This script must be executed in a PowerShell session running as Administrator." |
| 35 | } |
| 36 | |
| 37 | $ErrorActionPreference = 'Stop' |
| 38 | |
| 39 | # 1. Install OpenSSH Client |
| 40 | Write-Host "Checking OpenSSH Client status..." -ForegroundColor Cyan |
| 41 | $clientCapability = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Client*' |
| 42 | |
| 43 | if ($clientCapability.State -ne 'Installed') { |
| 44 | Write-Host "Installing OpenSSH Client..." -ForegroundColor Cyan |
| 45 | Add-WindowsCapability -Online -Name $clientCapability.Name | Out-Null |
| 46 | Write-Host "OpenSSH Client installed successfully." -ForegroundColor Green |
| 47 | } else { |
| 48 | Write-Host "OpenSSH Client is already installed." -ForegroundColor Yellow |
| 49 | } |
| 50 | |
| 51 | # 2. Install OpenSSH Server |
| 52 | Write-Host "Checking OpenSSH Server status..." -ForegroundColor Cyan |
| 53 | $serverCapability = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*' |
| 54 | |
| 55 | if ($serverCapability.State -ne 'Installed') { |
| 56 | Write-Host "Installing OpenSSH Server..." -ForegroundColor Cyan |
| 57 | Add-WindowsCapability -Online -Name $serverCapability.Name | Out-Null |
| 58 | Write-Host "OpenSSH Server installed successfully." -ForegroundColor Green |
| 59 | } else { |
| 60 | Write-Host "OpenSSH Server is already installed." -ForegroundColor Yellow |
| 61 | } |
| 62 | |
| 63 | # 3. Configure and Start SSHD Service |
| 64 | Write-Host "Configuring OpenSSH Server service (sshd)..." -ForegroundColor Cyan |
| 65 | Set-Service -Name sshd -StartupType 'Automatic' |
| 66 | |
| 67 | $sshdService = Get-Service -Name sshd |
| 68 | if ($sshdService.Status -ne 'Running') { |
| 69 | Start-Service sshd |
| 70 | Write-Host "OpenSSH Server service started." -ForegroundColor Green |
| 71 | } else { |
| 72 | Write-Host "OpenSSH Server service is already running." -ForegroundColor Yellow |
| 73 | } |
| 74 | |
| 75 | # 4. Verify/Create Firewall Rule |
| 76 | Write-Host "Verifying Firewall Configuration..." -ForegroundColor Cyan |
| 77 | if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) { |
| 78 | Write-Host "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..." -ForegroundColor Cyan |
| 79 | New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 | Out-Null |
| 80 | Write-Host "Firewall rule created successfully." -ForegroundColor Green |
| 81 | } else { |
| 82 | Write-Host "Firewall rule 'OpenSSH-Server-In-TCP' already exists." -ForegroundColor Yellow |
| 83 | } |
| 84 | |
| 85 | Write-Host "`nOpenSSH Client and Server installation and configuration complete!" -ForegroundColor Green |
ergosteur / Windows Scripts
Last active 1 month ago
Various scripts for automating Windows setup
Revision 8bb7864d9b5df9bf6b099f9c7c4cd96d00068982