When I first started working with Microsoft Azure Infrastructure services, I managed cloud resources exclusively using the Azure portal/dashboard. And since I could do pretty much everything I needed to do directly on the portal, I never bothered with Azure PowerShell or any of the other methods of communicating with Azure.
These days, however, I automate most of my Azure workloads using Azure PowerShell because I see now that PowerShell provides you with more options for managing Microsoft’s cloud. And using Azure PowerShell cmdlets and scripts is just plain faster and more productive in almost all cases.
If, like me, you work on a business that uses Azure, how do you manage your Azure services today? If you limit yourself to using the user interface on the Azure portal alone, you might be wasting a ton of time and missing out on options by not leveraging the quick, one-line or script management and automation capabilities of PowerShell.
In this article, I’ll be showing you how to get started managing Azure resources with PowerShell. We’ll install the Azure PowerShell module and connect to your Azure subscription.
In subsequent articles, I will be sharing Azure PowerShell commands for various cloud deployment workloads.
You might already be using PowerShell to automate other things in your environment. But what if you haven’t yet had the chance or you’ve just been too stubborn to try PowerShell for managing your Azure resources? Automation is the key to reducing human error, delivering a predictable service and having the time to do more with less! It’s time you take your cloud game to the next level and dive into managing Azure with PowerShell.
Is Azure PowerShell really necessary? Any other options?
Please note that it is not necessary to use PowerShell to interact with Microsoft Azure. Azure works well with PowerShell but does not require it. As earlier mentioned, Microsoft Azure can be managed directly through the Azure portal. And instead of PowerShell, you could also use bash (Azure CLI) as well. It’s mostly a matter of preference, or expertise, or use-case really.
It is also possible to call Azure Resource Manager directly via a REST API or via a declarative model called Azure Resource Manager templates. In most use cases however, PowerShell is just the right tool.
Windows PowerShell is a general purpose scripting language. It is quite popular and comes in handy to write administration scripts for Windows and it integrates well with .NET. PowerShell is open source as well and is available for Windows and most UNIX systems, including Linux.
Installing Azure PowerShell
Installing items from the PowerShell Gallery requires the PowerShellGet module. Make sure you have the appropriate version of PowerShellGet and other system requirements. In most cases, you would probably already have PowerShellGet installed. It is built into Windows 10.
Run the following command to see if you have PowerShellGet installed on your system.
Get-Module PowerShellGet -list | Select-Object Name,Version,Path
You should see something similar to the following output:
Name Version Path ---- ------- ---- PowerShellGet 188.8.131.52 C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\184.108.40.206\PowerShellGet.psd1
If you do not have PowerShellGet installed, see here.
Now that you have confirmed (or installed) PowerShellGet, let’s install Azure PowerShell.
Run the following command from an elevated PowerShell session:
Install-Module AzureRM -AllowClobber
If you have a version older than 220.127.116.11 of NuGet, you are prompted to download and install the latest version of NuGet. The message looks like this:
NuGet provider is required to continue PowerShellGet requires NuGet provider version '18.104.22.168' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\<YOUR USER NAME>\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 22.214.171.124 -Force'. Do you want PowerShellGet to install and import the NuGet provider now? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
Answer ‘Yes’ to continue with the installation of the latest version of NuGet.
By default, the PowerShell gallery is not configured as a Trusted repository for PowerShellGet. The first time you use the PSGallery you see the following prompt:
Untrusted repository You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y
Answer ‘Yes’ or ‘Yes to All’ to continue with the installation.
The AzureRM module is a rollup module for the Azure Resource Manager cmdlets. When you install the AzureRM module, any Azure PowerShell module not previously installed is downloaded and from the PowerShell Gallery.
Once the module is installed, you need to load the module into your PowerShell session. You should do this in a normal (non-elevated) PowerShell session. Modules are loaded using the Import-Module cmdlet, as follows:
When you run the above Import-Module command, you may hit an error like this:
Import-Module : File C:\Program Files\WindowsPowerShell\Modules\AzureRM\6.13.1\AzureRM.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 + Import-Module AzureRM + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [Import-Module], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand
To fix that, change the execution policy using this command (this should be run from an elevated PowerShell session):
Now go back to a non-elevated PowerShell session and run this command again:
The import should now work. Going forward, you can login to Azure from PowerShell using the Login-AzureRMAccount command. And then you can execute other commands to automate your cloud workload.
More About The Execution Policy
If you would rather not change the execution policy globally (like we did using the “Set-ExecutionPolicy RemoteSigned” command above), you can create a custom PowerShell shortcut instead.
To do this, just append “-ExecutionPolicy ByPass” to a PowerShell shortcut path (for example, an appropriately named desktop shortcut).
For the current version of PowerShell, the full shortcut path should now be:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass
Now within a PowerShell session that you opened using your new shortcut, run this line of code again:
The import should work and you can go ahead and run your other Azure PowerShell commands. Just remember to launch PowerShell using your custom shortcut so you don’t encounter AzureRM errors every time.