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! 🎉
This content originally appeared on DEV Community and was authored by Benly Walter

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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.