using namespace System.Management.Automation Register-ArgumentCompleter -CommandName ssh,scp,sftp -Native -ScriptBlock { param($wordToComplete, $commandAst, $cursorPosition) $knownHosts = Get-Content ${Env:HOMEPATH}\.ssh\known_hosts ` | ForEach-Object { ([string]$_).Split(' ')[0] } ` | ForEach-Object { $_.Split(',') } ` | Sort-Object -Unique # For now just assume it's a hostname. $textToComplete = $wordToComplete $generateCompletionText = { param($x) $x } if ($wordToComplete -match "^(?[-\w/\\]+)@(?[-.\w]+)$") { $textToComplete = $Matches["host"] $generateCompletionText = { param($hostname) $Matches["user"] + "@" + $hostname } } $knownHosts ` | Where-Object { $_ -like "${textToComplete}*" } ` | ForEach-Object { [CompletionResult]::new((&$generateCompletionText($_)), $_, [CompletionResultType]::ParameterValue, $_) } }