Exercise - Create an Azure Resource using scripts in Azure PowerShell

Recall our original scenario - creating VMs to test our CRM software. When a new build is available, we want to spin up a new VM so we can test the full install experience from a clean image. Then when we are finished, we want to delete the VM.

Let's try the commands you would use to create a VM.

Create a Linux VM with Azure PowerShell

Since we are using the Azure sandbox, you won't have to create a Resource Group. Instead, use the Resource Group [sandbox resource group name]. In addition, be aware of the location restrictions.

Let's create a new Azure VM with PowerShell.

  1. Use the New-AzVm cmdlet to create a VM.

    • Use the Resource Group [sandbox resource group name].

    • Give the VM a name - typically you want to use something meaningful that identifies the purposes of the VM, location, and (if there is more than one) instance number. We'll use "testvm-eus-01" for "Test VM in East US, instance 1". Come up with your own name based on where you place the VM.

    • Select a location close to you from the following list available in the Azure sandbox. Make sure to change the value in the below example command if you are using copy and paste.

      • westus2
      • southcentralus
      • centralus
      • eastus
      • westeurope
      • southeastasia
      • japaneast
      • brazilsouth
      • australiasoutheast
      • centralindia
    • Use "UbuntuLTS" for the image - this is Ubuntu Linux.

    • Use the Get-Credential cmdlet and feed the results into the Credential parameter.


      Please see the Linux VM FAQ for username and password limitations. Passwords must be 12 - 123 characters in length and meet 3 out of the following 4 complexity requirements:

      • Have lower characters
      • Have upper characters
      • Have a digit
      • Have a special character (Regex match [\W_])
    • Add the -OpenPorts parameter and pass "22" as the port - this will let us SSH into the machine.

    New-AzVm -ResourceGroupName <rgn>[sandbox resource group name]</rgn> -Name "testvm-eus-01" -Credential (Get-Credential) -Location "East US" -Image UbuntuLTS -OpenPorts 22


    You can use the Copy button to copy commands to the clipboard. To paste, right-click on a new line in the Cloud Shell window and select Paste or use the Shift+Insert keyboard shortcut (⌘+V on macOS).

  2. This will take a few minutes to complete. Once it does, you can query it and assign the VM object to a variable ($vm).

    $vm = Get-AzVM -Name "testvm-eus-01" -ResourceGroupName <rgn>[sandbox resource group name]</rgn>
  3. Then query the value to dump out the information about the VM:


    You should see something like:

    ResourceGroupName : <rgn>[sandbox resource group name]</rgn>
    Id                : /subscriptions/xxxxxxxx-xxxx-aaaa-bbbb-cccccccccccc/resourceGroups/<rgn>[sandbox resource group name]</rgn>/providers/Microsoft.Compute/virtualMachines/testvm-eus-01
    VmId              : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    Name              : testvm-eus-01
    Type              : Microsoft.Compute/virtualMachines
    Location          : eastus
    Tags              : {}
    HardwareProfile   : {VmSize}
    NetworkProfile    : {NetworkInterfaces}
    OSProfile         : {ComputerName, AdminUsername, LinuxConfiguration, Secrets}
    ProvisioningState : Succeeded
    StorageProfile    : {ImageReference, OsDisk, DataDisks}
  4. You can reach into complex objects through a dot (".") syntax. For example, to see the properties in the VMSize object associated with the HardwareProfile section you can type:

  5. Or to get information on one of the disks:

  6. You can even pass the VM object into other cmdlets. For example, this will retrieve the public IP address of your VM:

    $vm | Get-AzPublicIpAddress
  7. With the IP address, you can connect to the VM with SSH. For example, if you used the username "bob", and the IP address is "", then this command would connect to the Linux machine:

    ssh bob@

    Go ahead and log out by typing exit.

Delete a VM

Just to try out some more commands, let's delete the VM. We'll shut it down first.

Stop-AzVM -Name $vm.Name -ResourceGroup $vm.ResourceGroupName

Now, let's delete the VM with the Remove-AzVM cmdlet:

Remove-AzVM -Name $vm.Name -ResourceGroup $vm.ResourceGroupName

Try this command to list all the resources in your resource group:

Get-AzResource -ResourceGroupName $vm.ResourceGroupName | ft

You should see a bunch of resources (disks, virtual networks, etc.) that all still exist.


This is because the Remove-AzVM command just deletes the VM. It doesn't cleanup any of the other resources! At this point, we'd likely just delete the Resource Group itself and be done with it. However, let's just run through the exercise to clean it up manually. You should see a pattern in the commands.

  1. Delete the Network Interface.

    $vm | Remove-AzNetworkInterface –Force
  2. Delete the managed OS disks and storage account

    Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.OSDisk.Name | Remove-AzDisk -Force
  3. Next, delete the virtual network.

    Get-AzVirtualNetwork -ResourceGroup $vm.ResourceGroupName | Remove-AzVirtualNetwork -Force
  4. Delete the network security group.

    Get-AzNetworkSecurityGroup -ResourceGroup $vm.ResourceGroupName | Remove-AzNetworkSecurityGroup -Force
  5. And finally, the public IP address.

    Get-AzPublicIpAddress -ResourceGroup $vm.ResourceGroupName | Remove-AzPublicIpAddress -Force

We should have caught all the created resources; check the resource group just to be sure. We did a lot of manual commands here but a better approach would have been to write a script so we could reuse this logic later to create or delete a VM. Let's look at scripting with PowerShell.