Table of Contents
Bypassing the Azure Portal and going straight to PowerShell will provide you with more options for managing Microsoft’s cloud.
Installing the Azure PowerShell Module
To get started, you’ll need to download and install the Azure PowerShell module. This is a module provided by Microsoft.
1. Azure PowerShell requires your connecting machine to be running PowerShell version 5.0. To check, run command $PSVersionTable and confirm the Major build is 5.
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.19041.1682
2. Right click on Windows start icon then open Windows PowerShell Admin.
3. Run the following commands at once to install Azure PowerShell modules. This installation will take a few minutes. Once installed, you can connect your Azure subscription.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force;
Install-PackageProvider -Name NuGet -Force;
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted;
Install-Module -Name Az -AllowClobber -Scope CurrentUser
Connecting Your Azure Subscription
After you’ve gotten the Azure module installed, run this command Connect-AzAccount. This will bring up a small page asking you to input your account that has an active Azure subscription attached to it.
If all goes well, you should see an output showing your Account, Subscription and tenant information.
Connect-AzAccount
Account SubscriptionName TenantId Environment
------- ---------------- -------- -----------
[email protected] Azure Pass - Sponsorship dc65dd64-6886-40f2-b247-5b10d8fb5a66 AzureCloud
Once this happens, you’re ready to begin looking around to see what you can do. If you happen to have more than one subscription you can switch between them.
Starting/Stopping an Azure Iaas Virtual Machine
One of the most common Azure resources is its Infrastructure-as-a-service (IaaS) platform. If you’re already using Azure you most likely already have a few Azure VMs running in the cloud.
Let’s try to start and stop an existing VM from PowerShell. First, let’s just run Get-AzVM and check out all of the VMs we currently have running under your subscription.
Get-AzVM -Status
ResourceGroupName Name Location VmSize OsType Provisioning PowerState
----------------- ---- -------- ------ ------ ------------ ----------
AUSTRALIAEAST-RG vm-001 australiaeast Standard_B2s Windows Succeeded VM running
AUSTRALIAEAST-RG win11-001 australiaeast Standard_B2s Windows Succeeded VM running
You’ll see that I have two VMs and that the PowerState property is VM Running (it’s running). We”ll try to stop a VM using Stop-AzVM command.
Stop-AzVM -Name "vm-001" -ResourceGroupName "AUSTRALIAEAST-RG" -StayProvisioned
Virtual machine stopping operation
This cmdlet will stop the specified virtual machine. Do you want to continue?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
OperationId : ee000b52-b6e8-4c0a-a738-c64bc60a7530
Status : Succeeded
StartTime : 8/10/2022 9:00:36 PM
EndTime : 8/10/2022 9:01:12 PM
Error :
Get-AzVM -Status
ResourceGroupName Name Location VmSize OsType Provisioning PowerState
----------------- ---- -------- ------ ------ ------------ ----------
AUSTRALIAEAST-RG vm-001 australiaeast Standard_B2s Windows Succeeded VM stopped
AUSTRALIAEAST-RG win11-001 australiaeast Standard_B2s Windows Succeeded VM running
We now have a stopped VM. Let’s bring it back up again. This is the super-easy part. All we need to do is pipe our VM to Start-AzVM and we’re done!
Start-AzVM -Name "vm-001" -ResourceGroupName "AUSTRALIAEAST-RG"
OperationId : eaf6c3d6-2857-4c45-9b34-3a64ed525846
Status : Succeeded
StartTime : 8/10/2022 9:09:05 PM
EndTime : 8/10/2022 9:09:17 PM
Error :