Skip to content

Instantly share code, notes, and snippets.

@slardiere
Last active February 7, 2022 22:31
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slardiere/2db9f18babf9cc56598cb6d85ff9d02f to your computer and use it in GitHub Desktop.
Save slardiere/2db9f18babf9cc56598cb6d85ff9d02f to your computer and use it in GitHub Desktop.
Install OpenSSH on Windows
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
$url = 'https://github.com/PowerShell/Win32-OpenSSH/releases/latest/'
$request = [System.Net.WebRequest]::Create($url)
$request.AllowAutoRedirect=$false
$response=$request.GetResponse()
$file = $([String]$response.GetResponseHeader("Location")).Replace('tag','download') + '/OpenSSH-Win64.zip'
$client = new-object system.Net.Webclient;
$client.DownloadFile($file ,"c:\\OpenSSH-Win64.zip")
Unzip "c:\\OpenSSH-Win64.zip" "C:\Program Files\"
mv "c:\\Program Files\OpenSSH-Win64" "C:\Program Files\OpenSSH\"
powershell.exe -ExecutionPolicy Bypass -File "C:\Program Files\OpenSSH\install-sshd.ps1"
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
net start sshd
Set-Service sshd -StartupType Automatic
Set-Service ssh-agent -StartupType Automatic
cd "C:\Program Files\OpenSSH\"
Powershell.exe -ExecutionPolicy Bypass -Command '. .\FixHostFilePermissions.ps1 -Confirm:$false'
$registryPath = "HKLM:\SOFTWARE\OpenSSH\"
$Name = "DefaultShell"
$value = "C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe"
IF(!(Test-Path $registryPath))
{
New-Item -Path $registryPath -Force
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force
} ELSE {
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force
}
@thewh1teagle
Copy link

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment