Create a Storage Account On Azure With PowerShell

In this blog post, I will show you how to create a storage account on Microsoft Azure using PowerShell and check if the account exit or not.

The script uses variables to populate the cmdlet but also check if the Azure storage account and resource group name exist before creating the account.

In the code, I am assuming that you are also creating a new resource group as part of the process, if this is not the case, remove the resource group line.

Before you run the code, make sure you have Azure PowerShell installed.

Code

#variables
$rgname = "ntw2020"
$location = "westus"
$strname = "ntwstorage2020"
$skuname = "standard_lrs"
$tier = "hot"
#Check if RG is already exist in Azure
$x = Get-AzResourceGroup
foreach($y in $x){
if($y.ResourceGroupName -like $rgname)
{ 
write-host $y.ResourceGroupName "Resource Group name already exists in your subscription" -ForegroundColor Red
exit
}# 
Write-Host "checking Resource group name:" $y.ResourceGroupName -ForegroundColor Green 
}
#Check if storge account name is already exist in Azure
$z = Get-AzStorageAccount
foreach($y in $z){
if($z.storageaccountname -like $strname)
{ 
write-host $z.storageaccountname "Storage Account name already exists in your subscription" -ForegroundColor Red
exit
}# 
Write-Host "checking storage account name:" $z.storageaccountname -ForegroundColor Green 
}
New-AzResourceGroup -Name $rgname -Location $location -Verbose 
New-AzStorageAccount -ResourceGroupName $rgname -Name $strname -Location $location -SkuName $skuname -AccessTier $tier -Verbose

Processing…
Success! You're on the list.

Posted

in

, ,

by