Automate kubeconfig from Rancher with PowerShell

If you’re like me and manage multiple Kubernetes clusters using Rancher, you’ve probably run into situations where you needed to copy the kubeconfig from Rancher to your local machine — whether for debugging with kubectl or accessing clusters with k9s….


This content originally appeared on DEV Community and was authored by Benly Walter

If you're like me and manage multiple Kubernetes clusters using Rancher, you've probably run into situations where you needed to copy the kubeconfig from Rancher to your local machine — whether for debugging with kubectl or accessing clusters with k9s. Personally, I prefer managing everything through k9s and leaving the Rancher UI for developers or support teams who don’t interact with Kubernetes as deeply as I do.

In this post, I’ll show you how I automated this process with a PowerShell script so you can update your local kubeconfig for all clusters in one go.

What does this script do?

✅ Connects to multiple Rancher environments
✅ Lists all clusters (or filters by name/provider)
✅ Optionally backs up your existing kubeconfig
✅ Merges or recreates kubeconfig automatically
✅ Leaves you ready to kubectl get pods everywhere

Requirements

  • PowerShell Core
  • kubectl CLI installed and available in your PATH
  • Rancher API key. You can create a key with no scope which lets you manage all clusters where you have access.
  • Rancher module with helper functions:

    • Get-RancherClusterAll
    • Get-RancherClusterInfo
    • Get-RancherKubeConfig
    • Merge-KubeConfigFromRancher

Why PowerShell Core?

PowerShell Core works on Windows, macOS, and Linux. It’s perfect for cross-platform automation, and it makes interacting with APIs and files super smooth.

Example usage

$rancherAccounts = @{
    "dev" = @{
        url = "https://rancher-dev.example.com"
        token = "token-xxxxxx"
    }
    "prod" = @{
        url = "https://rancher-prod.example.com"
        token = "token-yyyyyy"
    }
}

# Backup and recreate existing Kubeconfig File
.\rancher-set-kubeconfig.ps1 -rancherAccounts $rancherAccounts -backupExisting -recreateConfig

# Specify Backup Path
.\rancher-set-kubeconfig.ps1 -rancherAccounts $rancherAccounts -backupExisting -backupPath "C:\Backups\KubeconfigBackups"

# Specify Custom Kubeconfig File Path on Windows
.\rancher-set-kubeconfig.ps1 -rancherAccounts $rancherAccounts -kubeConfigFilePath "C:\Users\<user>\.kube\custom-config"

# Select Specific Clusters
.\rancher-set-kubeconfig.ps1 -rancherAccounts $rancherAccounts -clusters @("cluster1", "cluster2", "cluster3")

# Filter by Provider
.\rancher-set-kubeconfig.ps1 -rancherAccounts $rancherAccounts -provider "aks"

How it works

  • Takes in a list of Rancher accounts and optional filters (clusters, provider)
  • Figures out your OS-specific kubeconfig location
  • Backs up or removes existing config (if you want)
  • Fetches and merges all selected cluster configs from Rancher

Conclusion

This script has saved me hours of boring manual work and avoided nasty copy-paste errors in my kubeconfig.
If you'd like to try it, grab the full script here. Please note that the script references the rancher.psm1 module saved in the same path.
So far, the script has only been tested on Windows. If you find it useful or have ideas to improve it, feel free to share your thoughts in the comments or share it with others!

Automate your workflow, sit back, and sip some tea while it does the work for you! 🎉

Sip some tea


This content originally appeared on DEV Community and was authored by Benly Walter


Print Share Comment Cite Upload Translate Updates
APA

Benly Walter | Sciencx (2025-07-13T22:56:55+00:00) Automate kubeconfig from Rancher with PowerShell. Retrieved from https://www.scien.cx/2025/07/13/automate-kubeconfig-from-rancher-with-powershell/

MLA
" » Automate kubeconfig from Rancher with PowerShell." Benly Walter | Sciencx - Sunday July 13, 2025, https://www.scien.cx/2025/07/13/automate-kubeconfig-from-rancher-with-powershell/
HARVARD
Benly Walter | Sciencx Sunday July 13, 2025 » Automate kubeconfig from Rancher with PowerShell., viewed ,<https://www.scien.cx/2025/07/13/automate-kubeconfig-from-rancher-with-powershell/>
VANCOUVER
Benly Walter | Sciencx - » Automate kubeconfig from Rancher with PowerShell. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/13/automate-kubeconfig-from-rancher-with-powershell/
CHICAGO
" » Automate kubeconfig from Rancher with PowerShell." Benly Walter | Sciencx - Accessed . https://www.scien.cx/2025/07/13/automate-kubeconfig-from-rancher-with-powershell/
IEEE
" » Automate kubeconfig from Rancher with PowerShell." Benly Walter | Sciencx [Online]. Available: https://www.scien.cx/2025/07/13/automate-kubeconfig-from-rancher-with-powershell/. [Accessed: ]
rf:citation
» Automate kubeconfig from Rancher with PowerShell | Benly Walter | Sciencx | https://www.scien.cx/2025/07/13/automate-kubeconfig-from-rancher-with-powershell/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.