Azure PowerShell Quick Start Guide
This guide will walk you through the essential steps to get started with Azure PowerShell, enabling you to manage your Azure resources efficiently from the command line.
Prerequisites
Before you begin, ensure you have the following:
- An active Azure subscription. If you don't have one, you can sign up for a free trial.
- Windows PowerShell 5.1 or later, or PowerShell Core (6.x and later) installed on your machine.
Step 1: Install Azure PowerShell Modules
The Azure PowerShell module contains all the cmdlets you'll need to interact with Azure. You can install it using PowerShell.
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -ForceThis command installs the latest version of the Az module for the current user. If you need to install it for all users, you might need administrator privileges.
Verify Installation
After installation, you can verify it by importing the module and checking its version:
Import-Module Az
Get-Module AzStep 2: Connect to Your Azure Account
Once the module is installed, you need to connect to your Azure account to authorize Azure PowerShell to access your subscription.
Connect-AzAccountThis command will open a browser window or prompt you to sign in with your Azure credentials. After successful authentication, you'll be connected.
Selecting a Subscription
If you have multiple Azure subscriptions, you can select the one you want to use:
# List all subscriptions
Get-AzSubscription
# Set the context to a specific subscription by its ID
Set-AzContext -SubscriptionId "your-subscription-id"Step 3: Create Your First Azure Resource
Let's create a resource group, which is a logical container for your Azure resources.
- 
                        Create a Resource GroupUse the New-AzResourceGroupcmdlet to create a new resource group.New-AzResourceGroup -Name "MyResourceGroup" -Location "East US"Replace MyResourceGroupwith your desired name andEast USwith your preferred Azure region.
- 
                        Create a Virtual MachineNow, let's deploy a simple virtual machine within the resource group. Note: Creating a VM involves several parameters. This example creates a basic Ubuntu VM. Refer to the official documentation for more advanced configurations. $rgName = "MyResourceGroup" $vmName = "MyVM" $location = "East US" $vmSize = "Standard_DS1_v2" $osType = "Linux" $adminUser = "azureuser" $sshKey = "ssh-rsa AAAAB3NzaC1yc2E..." # Replace with your actual SSH public key # Create a public IP address $publicIp = New-AzPublicIpAddress -Name "${vmName}-pip" -ResourceGroupName $rgName -Location $location -AllocationMethod Dynamic # Create a virtual network $vnet = New-AzVirtualNetwork -Name "${vmName}-vnet" -ResourceGroupName $rgName -Location $location -AddressPrefix "10.0.0.0/16" $subnet = Add-AzVirtualSubnet -VirtualNetwork $vnet -Name "default" -AddressPrefix "10.0.0.0/24" # Create a network interface card (NIC) $nic = New-AzNetworkInterface -Name "${vmName}-nic" -ResourceGroupName $rgName -Location $location -SubnetId $subnet.Id -PublicIpAddressId $publicIp.Id # Create a Linux virtual machine $vmConfig = New-AzVMConfig -VMName $vmName -VMSize $vmSize $vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Linux -ComputerName $vmName -Credential (Get-Credential) # You will be prompted for password if not using SSH key $vmConfig = Set-AzVMSourceImage -VM $vmConfig -PublisherName Canonical -Offer UbuntuServer -Sku 18.04-LTS -Version latest $vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id # Create the VM New-AzVM -ResourceGroupName $rgName -Location $location -VM $vmConfig Write-Host "Virtual machine '$vmName' created successfully in resource group '$rgName'." Write-Host "Public IP address: $($publicIp.IpAddress)"This script performs the following: - Defines variables for resource names, location, and VM size.
- Creates a public IP address.
- Sets up a virtual network and subnet.
- Configures a network interface with the public IP and subnet.
- Defines the VM configuration, including OS image (Ubuntu 18.04 LTS in this case) and network interface.
- Creates the virtual machine using New-AzVM.
 You will be prompted to enter credentials for the VM's administrator user. If you want to use SSH keys, you'll need to generate them and provide the public key content. 
Step 4: Manage Your Resources
Once your resources are deployed, you can manage them using various Azure PowerShell cmdlets.
List Resources
To list resources in a resource group:
Get-AzResource -ResourceGroupName "MyResourceGroup"Stop and Start a VM
To stop a virtual machine:
Stop-AzVM -ResourceGroupName "MyResourceGroup" -Name "MyVM" -ForceTo start a virtual machine:
Start-AzVM -ResourceGroupName "MyResourceGroup" -Name "MyVM"Delete Resources
To delete the resource group and all its contents:
Remove-AzResourceGroup -Name "MyResourceGroup" -Force -AsJobUsing -AsJob allows the deletion to run in the background.
Next Steps
This quick start guide provides a basic introduction. Azure PowerShell offers a vast array of cmdlets for managing every aspect of your Azure environment.