Trying to get the auto-complete feature of kubectl working with an alias of k, using Windows PowerShell and PowerShell Core on Windows Server 2022, but can't get it to work
tested with
echo 'Set-Alias -Name k -Value kubectl' >> $PROFILE
kubectl completion powershell >> $PROFILE
or even simply this to try to get it working in the same session, without worrying about saving it to the PowerShell profile
Set-Alias -Name k -Value kubectl
kubectl completion powershell | Out-String | Invoke-Expression
Results so far:
Powershell 5.1
Tab completion with kubectl: OK
Tab completion with k: Fails
Powershell Core 7.3
Tab completion with kubectl: Fails
Tab completion with k: Fails
The results above are tested with both Windows Terminal and cmd
Am I missing something obvious? (like I normally do)
Thanks!
PowerShell completions by alias in cobra supported only from version 1.6.0. kubectl depends on cobra 1.6.0 since v1.26 version. So, you should use kubectl v1.26+ and powershell 5.0+
I use it with powershell 7.3, kubectl v1.26.1 in windows 10 but it suggest variants only after typing first letter (it doesn't show select menu without typing any letter)
Set-Alias -Name k -Value kubectl
kubectl completion powershell | Out-String | Invoke-Expression
Register-ArgumentCompleter -CommandName k -ScriptBlock $__kubectlCompleterBlock
                        So go it working on PowerShell 5.1 with this, and assuming that Cobra only works on Powershell 5.x as per this, so given up on getting PowerShell Core working!
# Powershell 5.x
echo 'Set-Alias -Name k -Value kubectl' >> $PROFILE
echo 'Register-ArgumentCompleter -CommandName k -ScriptBlock $__kubectlCompleterBlock'  >> $PROFILE
kubectl completion powershell >> $PROFILE
# Reload the PowerShell profile
. $PROFILE
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With