How to Setup kubectl Autocomplete in Windows Powershell

To set up kubectl autocompletion on Windows, follow these steps:

Prerequisites

  • Ensure that you have kubectl installed. If not, you can download and install it from the official Kubernetes install page.

  • Ensure that you have PowerShell installed. It comes pre-installed with Windows, but if you need a different version, you can download it from the PowerShell GitHub page.

Steps

1. Open PowerShell

Press Win + x and select “Windows PowerShell” or “Windows PowerShell (Admin)”.

2. Install kubectl autocomplete script

Execute the following commands in your PowerShell session to enable kubectl autocompletion:

kubectl completion powershell | Out-String | Invoke-Expression

3. Persist the autocompletion script

To ensure the autocomplete script runs in every new PowerShell session, add the completion script to your PowerShell profile. The PowerShell profile is a script that runs every time PowerShell starts.

To find the location of your PowerShell profile script, run:

$PROFILE

If the profile does not exist, you need to create it. To do this, run:

if (!(Test-Path -Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }

Open your PowerShell profile script in a text editor:

notepad $PROFILE

Add the following line to the profile script and save the file:

kubectl completion powershell | Out-String | Invoke-Expression

4. Reload your PowerShell profile

After saving the changes, reload your profile by running:

. $PROFILE

Verification

To verify that the autocompletion is working, you can start typing a kubectl command and press Tab to see if it suggests completions. For example, type kubectl get po and then press Tab to autocomplete the word pods.

That’s it! You have successfully set up kubectl autocompletion in PowerShell on Windows.

Leave a Reply