Kubectl Autocomplete in PowerShell

A kubectl autocomplete is a feature that helps to complete kubectl commands or filenames automatically by pressing the ↹ Tab key.

This feature can be enabled in Windows PowerShell by invoking a simple kubectl completion powershell command.

Though this way it will be enabled temporary for the current session only.

To enable the kubectl autocomplete feature permanently it is required to add some commands to your Powershell $PROFILE file and this post shows how to do this.

Kubectl Autocomplete in PowerShell

Execute the following commands to enable the kubectl autocomplete feature in Powershell and make it permanent:

PS C:\> kubectl completion powershell >> $PROFILE
PS C:\> . $PROFILE

If you use the kubectl command frequently you may also find it useful to create a shorter and easier to type alias for it, such as k:

PS C:\> echo 'Set-Alias -Name k -Value kubectl' >> $PROFILE
PS C:\> echo 'Register-ArgumentCompleter -CommandName k -ScriptBlock $__kubectlCompleterBlock' >> $PROFILE
PS C:\> . $PROFILE

The kubectl alias k will help you to save time and effort when typing:

PS C:\> k version --client --output=yaml
- sample output -
clientVersion:
  buildDate: "2023-07-19T12:20:54Z"
  compiler: gc
  gitCommit: fa3d7990104d7c1f16943a67f11b154b71f6a132
  gitTreeState: clean
  gitVersion: v1.27.4
  goVersion: go1.20.6
  major: "1"
  minor: "27"
  platform: windows/amd64
kustomizeVersion: v5.0.1

The kubectl autocomplete feature with the k alias will help you to work more efficiently and reduce the risk of typos in PowerShell.

If while loading Powershell $PROFILE you get an error message that says “cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.“, you would have to change the default PowerShell execution policy.

Was it useful? Share this post with the world!

Leave a Reply