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:
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.
Microsoft Azure PowerShell 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:
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:
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:
To get all available commands in the Azure CmdLets, use the following:
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.