As for every Microsoft product, every task in Microsoft Azure can be scripted using PowerShell. This article will help you get started with the Azure CmdLets.
Checking and downloading the PowerShell Tools
To start off, you’ll need the Azure PowerShell CmdLets installed on your system to be able to run any PowerShell scripts against Azure. To check if the Azure modules are available on your system, you can use the following command:
Get-Module -ListAvailable -Name Azure*
The check is simple; if there’s no output, the Azure PowerShell tools aren’t installed. If they are installed, it looks something like this:
PS C:\Data> Get-Module -ListAvailable -Name Azure* Directory: C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Manifest 0.0.1 AzureRM.AnalysisServices {Resume-AzureRmAnalysisServicesServer, Suspend-AzureRmAnal... Manifest 3.1.0 AzureRM.ApiManagement {Add-AzureRmApiManagementRegion, Get-AzureRmApiManagementS... Manifest 2.3.0 AzureRM.Automation {Get-AzureRMAutomationHybridWorkerGroup, Get-AzureRmAutoma... Manifest 2.3.0 AzureRM.Backup {Backup-AzureRmBackupItem, Enable-AzureRmBackupContainerRe... Manifest 2.3.0 AzureRM.Batch {Remove-AzureRmBatchAccount, Get-AzureRmBatchAccount, Get-... ...
In case the tools aren’t installed, they will need to be downloaded and installed. To download the tools, head over to https://azure.microsoft.com/en-us/downloads/ and scroll down to “Command-line tools”. Click the “Windows install” link under “PowerShell” to start the download.
Once downloaded, start the install. The installation uses Web Platform Installer and is straightforward; just press “Install”, accept the license terms and installation will start. After installation is done, hit “Finish” and click “Exit” to exit the Web Platform Installer.
To be able to use the Azure PowerShell CmdLets, you’ll either need to reboot your machine, or import the modules, using the Import-Module CmdLet, registering each PSD1 file in the installation directory:
Get-ChildItem -Path "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\" -Filter *.psd1 -Recurse | Import-Module
Loading of some PowerShell modules will throw an error saying there are no valid module files in the directory; those can be ignored. Note that I’m using a 64-bit machine in my example, if you’re using a 32-bit OS, you’ll need to replace “Program Files (x86)” by “Program Files”.
Start using the Azure CmdLets
The first step in any commands executed against Azure, is logon to your Azure account. This can be done using the Login-AzureRmAccount CmdLet. When executed without any parameters, it will ask you for your Azure credentials. If successful, it will display information about your subscription:
PS C:\Data> Login-AzureRmAccount Environment : AzureCloud Account : f.vanderploeg@xxxxxxx.xxx TenantId : 12345678-aaaa-1b2c-1234-1234567890 SubscriptionId : 12345678-aaaa-1b2c-1234-1234567890 SubscriptionName : Pay-As-You-Go CurrentStorageAccount :
You can login using credentials, by passing the -Credential parameter. This causes the CmdLet to use the credentials (either passed by Get-Credential, or stored using a secure string) passed to the parameter:
Login-AzureRmAccount -Credential (Get-Credential)
Note that using passed credentials only works for Work or School accounts, not for personal accounts!
Exploring the Azure CmdLets
Now that you’ve logged on to your Azure account, you can start exploring the Azure environment using all PowerShell CmdLets. For example, to get a list of ALL your Azure resources, you can use the following CmdLet:
Get-AzureRmResource
To get all available commands in the Azure CmdLets, use the following:
Get-Command -Noun AzureRm*
Note that I used “AzureRm*” instead of “Azure*”; all commands without “Rm” refer to “Classic” deployments. To read about the differences between Resource Manager and Service Manager (or Classic) deployments, check out this page: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-deployment-model.
Now start exploring the available CmdLets and script your Azure environment. You can expect more articles which cover Azure PowerShell CmdLets in the future. I hope this article was of any use for you. Feel free to contact me through email, or leave a comment below.