Copy Key Vault secrets to another Key Vault
- Sanjiv Sidhar
- Jan 14, 2022
- 1 min read
Updated: May 13, 2024
Azure does provide a way to backup key vault secrets individually but does not allow back up of an entire key vault in a single operation.
I had the need to copy secrets from one key vault to another, from a key vault in test environment to a key vault in another environment. To facilitate this I used the following PowerShell scripts:
$sourceKvName = "kv-source-uks"
$destKvName = "kv-destination-uks"
$secretNames = (Get-AzKeyVaultSecret -VaultName $sourceKvName).Name
$secretNames.foreach{
Set-AzKeyVaultSecret -VaultName $destKvName -Name $_ `
-SecretValue (Get-AzKeyVaultSecret -VaultName $sourceKvName -Name $_).SecretValue
}
All the secrets in the source key vault are obtained using the Get-AzKeyVaultSecret command and stored in the $secretNames array. The secrets (secret names with values) are then copied into the destination key vault using the Set-AzKeyVaultSecret command.