Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl alias and autocomplete not working for powershell

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!

like image 837
display_name Avatar asked Nov 01 '25 03:11

display_name


2 Answers

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
like image 198
Cloud66 Avatar answered Nov 03 '25 19:11

Cloud66


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
like image 37
display_name Avatar answered Nov 03 '25 18:11

display_name