Azure Azure CLI Azure Governance Azure PowerShell

Azure Governance: Rename an Azure Subscription with Azure CLI and PowerShell

[February 21, 2022 – Updated post]


This blog post will show you how you can change the name of an Azure Subscription with the use of Azure CLI and Azure PowerShell. You will also find an Azure PowerShell and CLI script which you can use to automate the renaming process of your Azure Subscriptions.

When a proper Azure governance model is followed, you can ensure that your teams are operating in a secure and compliant Azure environment during design, development and operations. In this way you can not only minimize operational and reputational risks, but it will also help you to secure and to remain in control of your Azure environment.

As part of your Azure governance strategy, and also as a good starting point when setting up any new Azure environment, you should use a consistent well-defined naming convention for all your Azure resources. And off course this also applies for the naming of your Azure subscriptions.

If you want to read some more about applying a good and useful Azure naming convention, then you can always check out the following Microsoft Docs links. Develop your naming and tagging strategy for Azure resources and Recommended abbreviations for Azure resource types, which are both part of Microsoft’s Cloud Adoption Framework (CAF).


Rename an Azure Subscription


Open Windows Terminal (Run as administrator) and run following Azure CLI login command.

## Sign in with Azure CLI

az login

When a browser opens, sign in with your account credentials.


Before you can rename any Azure subscription your first need to know it’s subscription Id. In order to get a list of all subscriptions in the tenant with their Subscription Id from the account who is currently logged in, you need to run the following Azure PowerShell cmdlet.

## Get a list of subscriptions for the logged in account with their subscription ID, subscription name, tenant ID and their current state

Get-AzSubscription


When you found the subscription Id for the subscription you want to rename in the list, copy it and run the below CLI command to rename the subscription.

In the CLI command you just need change “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx” with the copied subscription Id and “new subscription name” with your preferred new subscription name.

## Rename subscription

az account subscription rename --subscription-id "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" --name "new subscription name"


After running the above CLI command, you can check if the rename was successful by running the following Azure PowerShell cmdlet.

## Get a list of subscriptions for the logged in account with their subscription ID, subscription name, tenant ID and their current state

Get-AzSubscription


Off course, you can also use the Azure Portal to see if the renaming was successful. Just be aware that it can take up to 10 minutes before the naming is applied and visible.

When logged into the Azure Portal with your account, type sub into the global search box. Then select Subscriptions.


On the Subscriptions window you can then see if the renaming of the subscription was successful.



Azure PowerShell and CLI Script

To automate the renaming process of my Azure Subscriptions, I wrote the below Azure PowerShell and CLI script.

To use the script copy and save it as Rename-Azure-Subscriptions-with-Azure-CLI-and-PowerShell.ps1 or download it from GitHub. Then before using the script, adjust all variables and the number of Subscriptions to your use. After you made the necessary adjustments, run the customized script with Administrator privileges from Windows TerminalVisual Studio Code, or Windows PowerShell


<#

.SYNOPSIS

A script used to rename Azure Subscriptions with Azure CLI and PowerShell.

.DESCRIPTION

A script used to rename Azure Subscriptions with the use of Azure CLI and Azure PowerShell.

.NOTES

Filename:       Rename-Azure-Subscriptions-with-Azure-CLI-and-PowerShell.ps1
Created:        21/02/2022
Last modified:  21/02/2022
Author:         Wim Matthyssen
Action:         Change variables were needed to fit your needs. 
Disclaimer:     This script is provided "As IS" with no warranties.

.EXAMPLE

.\Rename-Azure-Subscriptions-with-Azure-CLI-and-PowerShell.ps1

.LINK

https://wmatthyssen.com/2021/04/21/azure-governance-rename-an-azure-subscription-with-azure-cli-and-powershell/
#>

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Sign in with Azure CLI (CLI)

az login

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Update Azure CLI to the latest version (CLI)

az upgrade

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Variables

$companyShortName = "<companyShortName>" # <your company short name here> Best is to use a three letter abbreviation. Example: "myh"

$productionShortName = "<production short name here>" # Best is to use a three letter abbreviation. Example: "prd"
$acceptanceShortName = "<acceptance short name here>" # Best is to use a three letter abbreviation. Example: "acc"
$developmentShortName = "<development short name here>" # Best is to use a three letter abbreviation. Example: "dev"
$testShortName = "<test short name here>" # Best is to use a three letter abbreviation. Example: "tst"

$subIdManagement = "<your Management Subcriprition ID here>" # Example: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$subIdIdentity = "<your Identity Subcriprition ID here>" # Example: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$subIdConnectivity = "<your Connectivity Subcriprition ID here>" # Example: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$subIdProduction = "<your Production Subcriprition ID here>" # Example: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$subIdAcceptance = "<your Acceptance Subcriprition ID here>" # Example: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$subIdDevelopment = "<your Development Subcriprition ID here>" # Example: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$subIdTest = "<your Test Subcriprition ID here>" # Example: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

$subNameManagement = "sub-management-" + $companyShortName + "-01"
$subNameIdentity = "sub-identity-" + $companyShortName + "-01"
$subNameConnectivity = "sub-connectivity-" + $companyShortName + "-01"
$subNameProduction = "sub-" + $productionShortName + "-" + $companyShortName + "-01"
$subNameAcceptance = "sub-" + $acceptanceShortName + "-" + $companyShortName + "-01"
$subNameDevelopment = "sub-" + $developmentShortName + "-" + $companyShortName + "-01"
$subNameTest = "sub-" + $testShortName + "-" + $companyShortName + "-01"

$global:currenttime= Set-PSBreakpoint -Variable currenttime -Mode Read -Action {$global:currenttime= Get-Date -UFormat "%A %m/%d/%Y %R"}
$foregroundColor1 = "Red"
$foregroundColor2 = "Yellow"
$writeEmptyLine = "`n"
$writeSeperatorSpaces = " - "

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Start script execution

Write-Host ($writeEmptyLine + "# Script started. Without any errors, it will need around 6 minutes to complete" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine 
 
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Rename subscriptions (CLI)

az account subscription rename --subscription-id $subIdManagement --name $subNameManagement
az account subscription rename --subscription-id $subIdIdentity --name $subNameIdentity
az account subscription rename --subscription-id $subIdConnectivity --name $subNameConnectivity
az account subscription rename --subscription-id $subIdProduction --name $subNameProduction
az account subscription rename --subscription-id $subIdAcceptance --name $subNameAcceptance
az account subscription rename --subscription-id $subIdDevelopment --name $subNameDevelopment
az account subscription rename --subscription-id $subIdTest --name $subNameTest

Write-Host ($writeEmptyLine + "# All subscriptions are renamed" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Write script completed

Write-Host ($writeEmptyLine + "# Script completed" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



Conclusion

As part of your Azure Governance strategy, you should use a good and well-defined naming convention for your Azure Subscriptions, just like for any other Azure resource.

To apply your naming convention to your Azure Subscriptions you can use the Azure Portal, but as shown in this blog post you can also use Azure CLI and Azure PowerShell to rename them.


%d bloggers like this: