| 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 | irm https://1qaz.ca/install-openssh-win | iex |
| 13 | Downloads and executes the OpenSSH setup remotely via an elevated PowerShell session. |
| 14 | |
| 15 | .EXAMPLE |
| 16 | .\install-openssh.ps1 |
| 17 | Executes the script locally from an elevated PowerShell session. |
| 18 | |
| 19 | .NOTES |
| 20 | Prerequisites: |
| 21 | - Must run in an Elevated PowerShell Session (Run as Administrator). |
| 22 | - Requires Windows 10 (1809+) or Windows 11 with internet or WSUS access for Windows Capabilities. |
| 23 | |
| 24 | Key Features: |
| 25 | - Idempotent Execution: Safely skips installation/configuration steps if components are already present. |
| 26 | - Automated Service Setup: Configures `sshd` startup type to 'Automatic' and ensures it is running. |
| 27 | - Firewall Provisioning: Verifies or generates the 'OpenSSH-Server-In-TCP' inbound firewall rule. |
| 28 | |
| 29 | .LINK |
| 30 | https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse |
| 31 | #> |
| 32 | |
| 33 | # Requires Elevated PowerShell Session |
| 34 | if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { |
| 35 | throw "This script must be executed in a PowerShell session running as Administrator." |
| 36 | } |
| 37 | |
| 38 | $ErrorActionPreference = 'Stop' |
| 39 | |
| 40 | # 1. Install OpenSSH Client |
| 41 | Write-Host "Checking OpenSSH Client status..." -ForegroundColor Cyan |
| 42 | $clientCapability = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Client*' |
| 43 | |
| 44 | if ($clientCapability.State -ne 'Installed') { |
| 45 | Write-Host "Installing OpenSSH Client..." -ForegroundColor Cyan |
| 46 | Add-WindowsCapability -Online -Name $clientCapability.Name | Out-Null |
| 47 | Write-Host "OpenSSH Client installed successfully." -ForegroundColor Green |
| 48 | } else { |
| 49 | Write-Host "OpenSSH Client is already installed." -ForegroundColor Yellow |
| 50 | } |
| 51 | |
| 52 | # 2. Install OpenSSH Server |
| 53 | Write-Host "Checking OpenSSH Server status..." -ForegroundColor Cyan |
| 54 | $serverCapability = Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*' |
| 55 | |
| 56 | if ($serverCapability.State -ne 'Installed') { |
| 57 | Write-Host "Installing OpenSSH Server..." -ForegroundColor Cyan |
| 58 | Add-WindowsCapability -Online -Name $serverCapability.Name | Out-Null |
| 59 | Write-Host "OpenSSH Server installed successfully." -ForegroundColor Green |
| 60 | } else { |
| 61 | Write-Host "OpenSSH Server is already installed." -ForegroundColor Yellow |
| 62 | } |
| 63 | |
| 64 | # 3. Configure and Start SSHD Service |
| 65 | Write-Host "Configuring OpenSSH Server service (sshd)..." -ForegroundColor Cyan |
| 66 | Set-Service -Name sshd -StartupType 'Automatic' |
| 67 | |
| 68 | $sshdService = Get-Service -Name sshd |
| 69 | if ($sshdService.Status -ne 'Running') { |
| 70 | Start-Service sshd |
| 71 | Write-Host "OpenSSH Server service started." -ForegroundColor Green |
| 72 | } else { |
| 73 | Write-Host "OpenSSH Server service is already running." -ForegroundColor Yellow |
| 74 | } |
| 75 | |
| 76 | # 4. Verify/Create Firewall Rule |
| 77 | Write-Host "Verifying Firewall Configuration..." -ForegroundColor Cyan |
| 78 | if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) { |
| 79 | Write-Host "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..." -ForegroundColor Cyan |
| 80 | New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 | Out-Null |
| 81 | Write-Host "Firewall rule created successfully." -ForegroundColor Green |
| 82 | } else { |
| 83 | Write-Host "Firewall rule 'OpenSSH-Server-In-TCP' already exists." -ForegroundColor Yellow |
| 84 | } |
| 85 | |
| 86 | 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 768d310a4988aacba77f8cdc1aeb6137ebe3cb7f
| 1 | <# |
| 2 | .SYNOPSIS |
| 3 | Installs the complete Remote Server Administration Tools (RSAT) suite on Windows 10/11. |
| 4 | |
| 5 | .DESCRIPTION |
| 6 | Automates the discovery and installation of all available Remote Server Administration Tools |
| 7 | (RSAT) Windows Capabilities on Windows 10 (1809+) and Windows 11. |
| 8 | |
| 9 | The script queries all RSAT features available on demand via Windows Update/WSUS, identifies |
| 10 | those not currently enabled, and sequentially provisions every missing administration tool. |
| 11 | |
| 12 | .EXAMPLE |
| 13 | irm https://gist.githubusercontent.com/raw/YOUR_GIST_ID/install-rsat.ps1 | iex |
| 14 | irm https://1qaz.ca/install-rsat | iex |
| 15 | Downloads and executes the full RSAT setup remotely via an elevated PowerShell session. |
| 16 | |
| 17 | .EXAMPLE |
| 18 | .\install-rsat.ps1 |
| 19 | Executes the script locally from an elevated PowerShell session. |
| 20 | |
| 21 | .NOTES |
| 22 | Prerequisites: |
| 23 | - Must run in an Elevated PowerShell Session (Run as Administrator). |
| 24 | - Requires Windows 10 Pro/Enterprise/Education (1809+) or Windows 11 Pro/Enterprise/Education. |
| 25 | - Requires active internet connectivity or local access to Features on Demand (FOD). |
| 26 | |
| 27 | Key Features: |
| 28 | - Idempotent Execution: Scans existing system capabilities and skips tools already installed. |
| 29 | - Full Suite Provisioning: Targets all RSAT components (AD DS, DNS, DHCP, Group Policy, Server Manager, WSUS, etc.). |
| 30 | - Granular Tracking: Provides real-time progress for each capability installation. |
| 31 | |
| 32 | .LINK |
| 33 | https://learn.microsoft.com/en-us/windows-server/administration/install-remote-server-administration-tools |
| 34 | #> |
| 35 | |
| 36 | # Requires Elevated PowerShell Session |
| 37 | if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { |
| 38 | throw "This script must be executed in a PowerShell session running as Administrator." |
| 39 | } |
| 40 | |
| 41 | $ErrorActionPreference = 'Stop' |
| 42 | |
| 43 | Write-Host "Querying available RSAT capabilities..." -ForegroundColor Cyan |
| 44 | $rsatCapabilities = Get-WindowsCapability -Name "Rsat*" -Online |
| 45 | |
| 46 | $missingCapabilities = $rsatCapabilities | Where-Object State -ne 'Installed' |
| 47 | $installedCount = ($rsatCapabilities | Where-Object State -eq 'Installed').Count |
| 48 | $missingCount = $missingCapabilities.Count |
| 49 | |
| 50 | Write-Host "Found $($rsatCapabilities.Count) total RSAT capabilities ($installedCount already installed, $missingCount missing)." -ForegroundColor Yellow |
| 51 | |
| 52 | if ($missingCount -eq 0) { |
| 53 | Write-Host "All RSAT tools are already installed on this machine." -ForegroundColor Green |
| 54 | exit |
| 55 | } |
| 56 | |
| 57 | Write-Host "Beginning installation of $missingCount missing RSAT tool(s)...`n" -ForegroundColor Cyan |
| 58 | |
| 59 | foreach ($tool in $missingCapabilities) { |
| 60 | # Parse readable module name from package ID |
| 61 | $displayName = $tool.Name.Split("~")[0].Replace("Rsat.", "").Replace(".Tools", "") |
| 62 | Write-Host "Installing: $displayName ($($tool.Name))..." -ForegroundColor Cyan |
| 63 | |
| 64 | try { |
| 65 | Add-WindowsCapability -Online -Name $tool.Name | Out-Null |
| 66 | Write-Host "Successfully installed $displayName." -ForegroundColor Green |
| 67 | } |
| 68 | catch { |
| 69 | Write-Host "Failed to install $displayName: $_" -ForegroundColor Red |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | Write-Host "`nFull RSAT installation process complete!" -ForegroundColor Green |