Azure PowerShell

Azure PowerShell

Overview

  • Azure PowerShell is a module for Windows PowerShell or PowerShell Core.

  • Allows you to connect to Azure subscriptions and manage resources.

  • Example: Creating a VM:

New-AzVm -ResourceGroupName "CrmTestingResourceGroup" -Name "CrmUnitTests" -Image "UbuntuLTS"
  • Can run in:

    • Interactive mode: One command at a time.

    • Scripting mode: Execute scripts containing multiple commands.

  • Az Module:

    • Replacement for the older AzureRM module.

    • Provides -Az cmdlets, backward compatible with -AzureRM.

    • Open-source: Azure PowerShell GitHub

    • Covers: Resource Groups, Storage, VMs, Azure AD, Containers, Machine Learning, etc.


PowerShell Cmdlets & Modules

  • Cmdlet: Single-purpose PowerShell command (verb-noun format, e.g., Get-Process).

  • Common verbs: get (retrieve), set (update), format (format output), out (redirect output).

  • Get-Help shows detailed cmdlet info:

Get-Help Get-ChildItem -Detailed
  • Modules are collections of cmdlets:

Get-Module

Demonstration: Install Az Module

  1. Open PowerShell as administrator.

  2. Set execution policy if needed:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
  1. Install the module:

Install-Module -Name Az -AllowClobber
  1. Install NuGet provider if prompted.

  2. Trust the repository when prompted.


Connect to Azure & Manage Resources

# Login
Connect-AzAccount
# Verify subscription
Get-AzSubscription

# Create a resource group
New-AzResourceGroup -Name <name> -Location <location>

# Verify resource group
Get-AzResourceGroup

# Remove resource group
Remove-AzResourceGroup -Name <name>


Azure CLI

Overview

  • Cross-platform command-line tool (Linux, macOS, Windows).

  • Can run interactively or via scripts.

  • Example: Restart a VM:

az vm restart -g MyResourceGroup -n MyVm
  • Commands are structured in groups and subgroups (e.g., az storage blob).


Finding Commands

# Search for commands
az find -q blob
# Get detailed help
az storage blob —help


Demonstration: Install & Use Azure CLI

Install on Windows:

  1. Go to Install Azure CLI Windows

  2. Run installer, accept license, click Install.

Verify Installation:

az --version

Login:

az login
  • Browser opens for authentication, or use https://aka.ms/devicelogin.

Create a Resource Group:

az group create --name <name> --location <location>

Verify Resource Group:

az group list
az group list --output table
az group list --query "[?name == '<rg name>']"

Key Differences

Feature Azure PowerShell Azure CLI
Platform PowerShell (Windows/macOS/Linux) Shell/Command line (Windows/Linux/macOS)
Syntax Verb-Noun (Get-AzVM) az <group> <command>
Scripts PowerShell scripts Shell scripts (Bash, CMD, PowerShell)
Use case Deeper integration with PowerShell, automation Cross-platform, scripting friendly, simple syntax