Skip to content

Instantly share code, notes, and snippets.

@Shotokhan
Created June 6, 2023 09:52
Show Gist options
  • Save Shotokhan/0084590ae7f845ba1a19eaed92dc2539 to your computer and use it in GitHub Desktop.
Save Shotokhan/0084590ae7f845ba1a19eaed92dc2539 to your computer and use it in GitHub Desktop.
List user@host SSH saved profiles in Windows SSH config for current user
$inputData = Get-Content "C:\Users\$Env:UserName\.ssh\config"
$pattern = 'User\s+([^\s]+)|Host\s+([^\s]+)'
$reg_matches = [regex]::Matches($inputData, $pattern, 'Multiline')
$hosts = @{}
$currentHost = ""
foreach ($match in $reg_matches) {
if ($match.Groups[1].Success) {
$user = $match.Groups[1].Value
$hosts[$currentHost] = $user
} elseif ($match.Groups[2].Success) {
$currentHost = $match.Groups[2].Value
}
}
foreach ($ssh_host in $hosts.GetEnumerator()) {
$output = "{0}@{1}" -f $ssh_host.Value, $ssh_host.Name
Write-Output $output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment