PowerShell/docs/new-ssh-key.md
2024-11-08 12:40:31 +01:00

1.8 KiB

The new-ssh-key.ps1 Script

This PowerShell script creates a new SSH key for the user.

Parameters

/home/markus/Repos/PowerShell/scripts/new-ssh-key.ps1 [<CommonParameters>]

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Example

PS> ./new-ssh-key.ps1
 New SSH key of Ed25519 type saved to ~/.ssh - your public key is:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILb8s5zU9YDApGQ82H45fMKVPMr5cw9fzh3PEBjZZ+Rm markus@PI

Notes

Author: Markus Fleschutz | License: CC0

https://github.com/fleschutz/PowerShell

Script Content

<#
.SYNOPSIS
        Creates a new SSH key
.DESCRIPTION
        This PowerShell script creates a new SSH key for the user.
.EXAMPLE
        PS> ./new-ssh-key.ps1
	✅ New SSH key of Ed25519 type saved to ~/.ssh - your public key is:
	    ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILb8s5zU9YDApGQ82H45fMKVPMr5cw9fzh3PEBjZZ+Rm markus@PI
.LINK
        https://github.com/fleschutz/PowerShell
.NOTES
        Author: Markus Fleschutz | License: CC0
#>

try {
	& ssh-keygen
	if ($lastExitCode -ne "0") { throw "ssh-keygen failed" }

	if (Test-Path "~/.ssh/id_ed25519.pub") {
		$publicKey = Get-Content "~/.ssh/id_ed25519.pub"
		$enc = "Ed25519"
	} elseif (Test-Path "~/.ssh/id_rsa.pub") {
		$publicKey = Get-Content "~/.ssh/id_rsa.pub"
		$enc = "RSA"
	} else {
		throw "No public key found."
	}
	"✅ New SSH key of $enc type saved to ~/.ssh - your public key is:"
	"   $publicKey"
	exit 0 # success
} catch {
	"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
	exit 1
}

(generated by convert-ps2md.ps1 as of 11/08/2024 12:40:20)