Azure PowerShell and Azure CLI

It’s impossible to memorize all commands in PowerShell and Azure CLI. Good news is that Microsoft follows a standard pattern

Open this page;

https://learn.microsoft.com/en-us/cli/azure/reference-index?view=azure-cli-latest

If we look at Azure CLI command for Virtual Machine, they follow this naming convention;

az vm list
az vm create
az vm delete

Azure CLI start with az vm and a verb. another example is;

az keyvault list
az keyvault create
az keyvault delete

for a more complicated example;

az network vnet list
az network vnet create
az network vnet delete

In above example vnet is a sub-component of netowrk. another example is;

az network vnet subnet list
az network vnet subnet create
az network vnt subnet delete

Where subnet is a sub-component of vnet and vnet is a sub-component of network.

Let’s go over PowerShell now;

https://learn.microsoft.com/en-us/powershell/module/az.keyvault/new-azkeyvault?view=azps-12.0.0

Get-AzVM
New-AzVM
Remove-AzVM

Verb is the first part of the word.

Get-AzVirtualNetwork
New-AzVirtualNetwork
Remove-AzVirtualNetwork
Get-AzVirtualNetworkSubnetConfig
New-AzVirtualNetworkSubnetConfig
Remove-AzVirtualNetworkSubnetConfig

Standard PowerShell that comes with windows doesn’t work. We need to upgrade it to PowerShell7 which is a cross-plateform scripting engine. There are 3 Azure specific modules for PowerShell that we need to install (We can avoid to install anything by using Azure Cloud shell);

For demo purpose, I am using Azure Cloud shell. The advantage is that i don’t need to login to my account because i am already logged in to Azure Portal. The response that i will get here is JSON formatted.

PowerShell

Microsoft has switched from old “Azure RM” to “Az” module. They don’t run side by side. To install a new AZ Module, run this command;

Run as administrator

Install-Module -Name Az-AllowClobber -Repository PSGallery -Force

To update module if you have already installed it;

Run as administrator
Update-Module -Name Az -AllowClobber -Repository PSGallery

To connect to Azure from workstation, use this command in PowerShell terminal;

connect-AzAccount -TenantId {GUID} (We don't need curly brackets)

To check latest version of PowerShell, follow this link;

To view installed Az module, use this command;

Get-InstalledModule -Name Az -AllVersions | Select-Object -Property Name, Version

This will list down Name and Version of Az Module. My list has Az Version 8.1.0 installed.

To list my web app, run this command in PowerShell terminal

Get-AzWebApp

To get a shorter version of output, run this command;

Get-AzWebApp | Select-Object Name, Location | ConvertTo-CSV -NoTypeInformation
FavoriteLoadingAdd to favorites
Spread the love

Author: Shahzad Khan

Software developer / Architect