| 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
| 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 | return |
| 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 |
| 1 | <# |
| 2 | .SYNOPSIS |
| 3 | Downloads, extracts, and registers Microsoft Sysinternals Suite system-wide. |
| 4 | |
| 5 | .DESCRIPTION |
| 6 | Automates downloading the latest Sysinternals Suite from Microsoft Live servers, |
| 7 | extracts it to 'C:\Tools\Sysinternals', and permanently adds the directory to |
| 8 | the system-level PATH environment variable if not already present. |
| 9 | |
| 10 | .EXAMPLE |
| 11 | irm https://1qaz.ca/install-sysinternals | iex |
| 12 | Downloads and executes the Sysinternals setup remotely via an elevated PowerShell session. |
| 13 | |
| 14 | .EXAMPLE |
| 15 | .\install-sysinternals.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 active internet access to live.sysinternals.com. |
| 22 | |
| 23 | Key Features: |
| 24 | - Target Directory: Installs to 'C:\Tools\Sysinternals'. |
| 25 | - PATH Integration: Automatically updates system environment variables for immediate CLI access. |
| 26 | - Idempotent Execution: Safe to re-run for updating existing Sysinternals tools. |
| 27 | |
| 28 | .LINK |
| 29 | https://learn.microsoft.com/en-us/sysinternals/ |
| 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 | $installDir = "C:\Tools\Sysinternals" |
| 40 | $zipUrl = "https://download.sysinternals.com/files/SysinternalsSuite.zip" |
| 41 | $tempZip = Join-Path $env:TEMP "SysinternalsSuite_$([Guid]::NewGuid().Guid).zip" |
| 42 | |
| 43 | # 1. Create Target Directory |
| 44 | if (-not (Test-Path -Path $installDir)) { |
| 45 | Write-Host "Creating installation directory at ${installDir}..." -ForegroundColor Cyan |
| 46 | New-Item -ItemType Directory -Path $installDir -Force | Out-Null |
| 47 | } |
| 48 | |
| 49 | try { |
| 50 | # 2. Download Sysinternals Suite |
| 51 | Write-Host "Downloading Sysinternals Suite from Microsoft..." -ForegroundColor Cyan |
| 52 | Invoke-WebRequest -Uri $zipUrl -OutFile $tempZip -UseBasicParsing |
| 53 | |
| 54 | # 3. Extract Archive |
| 55 | Write-Host "Extracting tools to ${installDir}..." -ForegroundColor Cyan |
| 56 | Expand-Archive -Path $tempZip -DestinationPath $installDir -Force |
| 57 | Write-Host "Extraction complete." -ForegroundColor Green |
| 58 | } |
| 59 | finally { |
| 60 | # Cleanup Zip File |
| 61 | if (Test-Path -Path $tempZip) { |
| 62 | Remove-Item -Path $tempZip -Force -ErrorAction SilentlyContinue |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | # 4. Add to System PATH Environment Variable |
| 67 | Write-Host "Checking System PATH configuration..." -ForegroundColor Cyan |
| 68 | $currentPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine) |
| 69 | |
| 70 | if ($currentPath -split ';' -notcontains $installDir) { |
| 71 | Write-Host "Adding ${installDir} to System PATH..." -ForegroundColor Cyan |
| 72 | $newPath = "$currentPath;$installDir" |
| 73 | [Environment]::SetEnvironmentVariable("Path", $newPath, [EnvironmentVariableTarget]::Machine) |
| 74 | |
| 75 | # Update current session environment path |
| 76 | $env:Path = "$env:Path;$installDir" |
| 77 | Write-Host "System PATH updated successfully." -ForegroundColor Green |
| 78 | } else { |
| 79 | Write-Host "${installDir} is already present in System PATH." -ForegroundColor Yellow |
| 80 | } |
| 81 | |
| 82 | Write-Host "`nSysinternals Suite installation complete! Run tools directly from any console (e.g., 'procexp', 'procmon')." -ForegroundColor Green |