GuidesMicrosoft Azure PowerShell Scripts and Commands

Microsoft Azure PowerShell Scripts and Commands

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Using PowerShell scripts and commands for quickly executing tasks in Windows operating systems offers a number of benefits over traditional scripting languages, such as JScript and VBScript. While PowerShell scripting originated as a means to efficiently manage Windows operating systems, Microsoft has since extended the capabilities of PowerShell to automate a range of other tasks and systems, including Azure, Microsoft’s cloud computing service. 

Azure PowerShell is a robust command line tool that uses scripts and cmdlets to manage and administer tasks within the virtual machine (VM). In today’s Server tutorial, we’ll examine some of the useful PowerShell cmdlets you can use to interact with Azure resources.

How to create an Azure Virtual Machine

If you need to create an Azure virtual machine with PowerShell for production or development use, you can use the New-AzVM cmdlet. You can specify parameters such as the location of VHD, memory, processors and more. To quickly create a virtual machine in Azure using the default configuration, execute the following PowerShell command:
Windows Server Tutorials

New-AzVM -ResourceGroupName "myResourceGroup -Name "myVM"

While this command creates a virtual machine quickly in the Resource Group specified in the “-ResourceGroupName” parameter, the next command will create a virtual machine in Azure from a managed image.

New-AzVM -ResourceGroupName "myResourceGroup" -Location "West-US" -Name "myVM" -ImageName "myImageDefinition"

Reporting Azure Virtual Machines

Activity reports in Azure virtual machines are useful for providing details on how many VMs have been deployed and are currently running. Microsoft provides the Get-AzVM PowerShell cmdlet, which can be used to report virtual machines from an Azure subscription and/or from an Azure resource group.

To get all the virtual machines from an Azure Resource Group, execute this PowerShell command:

Get-AzVM -ResourceGroupName "myResourceGroup"

To export output to a CSV file, add the Export-CSV cmdlet as shown in below:

Get-AzVM -ResourceGroupName "myResourceGroup" | Export-CSV C:TempAllAzureVMs.CSV -NoTypeInfo

In Get-AzVM, you can also specify the properties that you would like to be reported when querying virtual machines in Azure. For example, the following PowerShell command only collects the type of Operating System running on virtual machines:

Get-AzVM | Select Name,@{Name="OSType"; Expression={$_.StorageProfile.OSDisk.OSType}}

By using the method shown in the command above, you can access nested properties of Azure virtual machines.

If you wish to list all the virtual machines in an Azure subscription, execute the “Get-AzVM” command in an elevated PowerShell window. If you would like to list virtual machines from Resource Groups listed in a text file, using the following PowerShell script would work:

$ResGroups = "C:TempResGroups.TXT"
$ReportFile = "C:TempAllVMsInAzure.CSV"
$STR = "VM Name, Resource Group, Location, Status Code"
Add-Content $ReportFile $STR
Foreach ($ResGroup in Get-Content $ResGroups)
{
$allVMsInReg = Get-AzureRMVM -ResourceGroupName "$ResGroup"
ForEach ($AllVMs in $AllVMsInRes)
{
$VMLocation = $AllVMs.Location
$VMName = $AllVMs.Name
$VMStatus = $AllVMs.StatusCode

$STR = $VMName+","+$ResGroup+","+$VMLocation+","+$VMStatus
Add-Content $ReportFile $STR
}
}

When you have finished executing that script, a report file will be generated that contains the name of the virtual machine, the resource group name of the virtual machine, the virtual machine Azure location and the status code indicating the overall status of the virtual machine.

How to Start, Stop, and Restart Azure Virtual Machines

From time to time you may need to stop, start and restart Azure virtual machines in order to reduce billing costs. For example, you may not want to run development virtual machines if they are not in use. Similarly, if you have assigned temporary virtual machines to a third-party vendor, you may want to remove them on your end as quickly as possible.

To stop an Azure virtual machine, you can use the Stop-AzVM PowerShell cmdlet as shown in the following PowerShell command:

Stop-AzVM -ResourceGroupName "myResourceGroup" -Name "myVM"

The next PowerShell script can be used to stop specific virtual machines specified in a text file. Let’s assume the text file contains the list of virtual machines to be stopped. Once you have created the text file that contains the virtual machines to be stopped, run the following PowerShell script:

$VMFile = "C:TempAllVMsToStop.TXT"
Foreach ($ThisVM in Get-Content $VMFile)
{
Stop-AzureRMVM -Name $ThisVM
}

Starting an Azure virtual machine is similar to stopping an Azure virtual machine. However, you will of course need to use the Start-AzureRMVM PowerShell cmdlet in place of the Stop-AzureRMVM cmdlet as shown in the following command:

Start-AzVM -ResourceGroupName "myResourceGroup" -Name "myVM"

If you run into any issues with the Azure virtual machine and are not able to take control via the Azure management portal, or if you have any issues accessing the virtual machine, one solution is to restart it using the PowerShell command below:

Restart-AzVM -ResourceGroupName "myResourceGroup" -Name "myVM"

Tips on Managing Azure Virutal Machines

We provided some useful Azure PowerShell commands you can use when managing Azure virtual machines. Here’s a quick rundown of what we covered in this server tutorial: 

  • How to create a simple Azure virtual machine or an Azure virtual machine from a managed image. 
  • How to list all Azure virtual machines and/or virtual machines in resource groups specified in a text file. 
  • How to stop, start and restart Azure virtual machines with PowerShell commands.


Nirmal Sharma is a MCSEx3, MCITP and Microsoft MVP in Directory Services. He specializes in directory services, Microsoft Azure, Failover clusters, Hyper-V, System Center and Exchange Servers, and has been involved with Microsoft technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to Health Packs for ADHealthProf.ITDynamicPacks.Net solutions. Nirmal can be reached at nirmal_sharma@mvps.org.

Follow ServerWatch on Twitter and on Facebook

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories