Skip to content

Instantly share code, notes, and snippets.

Created March 29, 2015 16:35
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
ssh-copy-id for windows
# ssh-copy-id analog for Windows systems
# Script to install your public key on a remote machine
# The remote machine must accept password authentication,
# or one of the other keys in your Putty Agent, for this to work.
# (c) soar -
# Dialog for public key selection
Function Select-File()
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog;
$OpenFileDialog.Filter = "Public Keys (*.pub) | *.pub";
$OpenFileDialog.ShowHelp = $true; # Without this line - dialog not appears.. I don't understand why.
[void] $OpenFileDialog.ShowDialog();
Function ShowMessage($title, $content, $type)
[void][Windows.Forms.Messagebox]::show($content, $title, $type);
# Returns current script directory, plink.exe must be in same place
Function Get-ScriptDirectory
$Invocation = (Get-Variable MyInvocation -Scope 1).Value;
Split-Path $Invocation.MyCommand.Path;
$pubKeyFile = Select-File
if (! $pubKeyFile) {
ShowMessage 'Need to select file' 'Please, select public key file for upload to remote host' 'OK';
$pubKey = Get-Content -LiteralPath $pubKeyFile;
if (! ($pubKey -is [string]) -or ! $pubKey.StartsWith("ssh-")) {
ShowMessage "Wrong file?" "Selected file not looks like valid SSH public key. It must starts with `"ssh-`" and contain one line." "OK"
$plinkExecutable = Join-Path (Get-ScriptDirectory) "plink.exe";
if ($args.Length) {
foreach ($arg in $args) {
& $plinkExecutable -ssh $arg "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; echo `"$pubKey`" >> ~/.ssh/authorized_keys"
} else {
$hostname = Read-Host "Please enter target hostname (user@hostname):";
if ($hostname) {
& $plinkExecutable -ssh $hostname "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; echo `"$pubKey`" >> ~/.ssh/authorized_keys"
Write-Host "Press any key to continue …"
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment