Create virtual machines with the Azure PowerShell

Today in this article, we will see how to create virtual machines with the Azure PowerShell step by step. we will also cover a few best practices on managing the scripts and resources etc.
Updated October 2026: a New-AzVM simple vs long-form comparison, Trusted Launch and troubleshooting notes, and an FAQ.
We will cover the below aspects with this step by step article,
- Create Azure account
- Launch Azure cloud shell i.e powershell
- Create Resource group
- Create Virtual machine
- Connect to VM
- Deploy WebServer in VM
- Test you WebApp deployed in VM
- How to clean the resources
Getting started
Create Azure account
Before you could begin, please make sure you have an Azure cloud Active subscription.
Please visit: Create Azure account
Launch Azure cloud shell i.e powershell
Azure PowerShell is a useful utility designed for managing and administering Azure resources effectively from the CLI commands line. Use Azure PowerShell to automate anything in the Azure environment.

Alternatively, cloud shell can be launched using below,
https://shell.azure.com/powershell
CloudShell – Create Resource group
A resource group is a logical container holding your Azure resources that are deployed and managed.
Create an Azure resource group with New-AzResourceGroup.
Command
New-AzResourceGroup -Name TheCodeBuzz -Location 'EastUS'
Example

CloudShell – Create Virtual Machine
The Az.Compute module provides a cmdlet called New-AzVM, which you can run in Cloud Shell or after Connect-AzAccount, which helps you create the VM based on the inputs specified in the commands.
Step1-
Before creating VM, please create a credentials object containing a username and password for the administrator account of the Windows VM.

Command
New-AzVm
-ResourceGroupName 'thecodebuzz'
-Name 'devvm'
-Location 'Specifiy locaiton Ex. East US'
-VirtualNetworkName 'Vnet'
-SubnetName 'Subnet'
-SecurityGroupName 'specify security group name'
-PublicIpAddressName 'specify IP address'
-OpenPorts 80,3389

New-AzVM simple parameter set vs New-AzVMConfig: which should you use?
Use the simple parameter set of New-AzVM for quick, disposable VMs, like the one in this walkthrough. Switch to New-AzVMConfig when you need control over each resource, such as an existing network, a specific NIC or a hardened security profile.
What does the simple parameter set create for you?
In the simple parameter set, most arguments are optional and New-AzVM fills the gaps. If they don’t already exist, it creates a virtual network, a subnet, a network security group (NSG), a public IP address and a storage account for boot diagnostics. That’s why the command in this article works with so few inputs.
In plain English: The simple set is like booking a furnished flat: the bed, kettle and keys are already there. The long form is like renting an empty unit and bringing every piece yourself. You get exactly what you want, but you have to order each piece.
What does the long-form flow look like?
The long form builds a VM object in memory, then deploys it in one call. The steps are:
- New-AzVMConfig: creates the VM object with a name and size.
- Set-AzVMOperatingSystem: sets the OS type, computer name and credentials.
- Set-AzVMSourceImage: picks the publisher, offer, SKU and version.
- Add-AzVMNetworkInterface: attaches a NIC that you’ve created beforehand.
- New-AzVM -VM: deploys the finished object.
Which settings should you always set explicitly?
Pin these in every script, whichever approach you use. Defaults change between Az releases, and a script that relied on them can behave differently later.
- Size: always pass
-Size.New-AzVMprints a breaking-change warning that the default is moving from Standard_D2s_v3 to Standard_D2s_v5. - Image: use a versioned alias such as
Ubuntu2204, or a fullpublisher:offer:sku:versionURN. Aliases likeUbuntuLTS,CentOS,DebianandRHELare being removed, andUbuntuLTSpointed to a release that reached end of life in April 2023. The current Windows quickstart uses2022-datacenter-azure-edition. - Ports:
-OpenPorts 80,3389opens HTTP and RDP. Open only what you need. For a Linux VM that you reach over SSH, that means port 22 instead of 3389. - Credentials: pass a
Get-Credentialobject or aSecureString. Never put a plain-text password in a script that you commit.
How does Trusted Launch change VM creation?
Since Az 11.0.0, New-AzVM defaults to the Trusted Launch security type, with Secure Boot and vTPM enabled, when you don’t set one. Microsoft has also made it the default for new eligible Gen2 VMs across the portal, CLI and PowerShell, and lists Azure PowerShell 15.6.1 or newer as a requirement. Existing VMs and Gen1 VMs are unaffected.
The long-form flow doesn’t apply this for you. Call Set-AzVmSecurityProfile with -SecurityType TrustedLaunch, then Set-AzVmUefi with Secure Boot and vTPM enabled, before New-AzVM -VM.
Connect to VM
Let’s now Connect via SSH with the client
Open Cloudshell CLI command line lets upload the downloaded private key(This key was downloaded as part of VM creation)

Please run the below set of commands to connect.
ls chmod 400 azureuser.pem ssh -i vmfirst_key.pem azureuser@20.127.10.153
Deploy WebServer in VM
Let’s install NGNIX Webserver as the next step,
sudo apt-get -y update
sudo apt-get -y install nginx root@devvm:/home/thecodebuzz# echo "Hello TheCodeBuzz"> /var/www/html/index.html

Let’s verify installed Web application by hitting the IP,

References:
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published (2026) best practices and guidelines for software design and development.
Frequently asked questions
How do I create a Linux VM with an SSH key using New-AzVM?
Use a versioned image alias such as Ubuntu2204 and pass -SshKeyName with -GenerateSshKey. Azure then generates the key pair and stores the public key, or you can supply your own public key instead. You connect over SSH with the matching private key, so keep that file safe. This differs from the Windows flow, which prompts for a username and password through Get-Credential and connects over RDP. Avoid old aliases like UbuntuLTS, which were announced for removal.
How do I create an Azure VM without a public IP address?
Set -PublicIpAddressName to an empty string ("") in New-AzVM. Pair it with an existing vnet and subnet, so the VM lands in a network you already control. Without a public IP you can’t reach the VM from the internet, so you’ll need Azure Bastion, a VPN, or another VM inside the same network to connect.
How do I create a VM in an existing virtual network and subnet with New-AzVM?
Pass -VirtualNetworkName and -SubnetName with the names of the network you already have, in the same resource group and location as the VM. New-AzVM only creates network resources that don’t exist yet, so it reuses a vnet and subnet it finds by name. If a name has a typo, expect a new vnet to be created instead of an error. Check the names with Get-AzVirtualNetwork before you run the command.
How do I delete the VM and all its resources afterwards?
Delete the whole resource group with Remove-AzResourceGroup -Name TheCodeBuzz. This removes the VM, NIC, disk, public IP, NSG, vnet and the diagnostics storage account inside it. It removes everything in the group, so only use it when the group holds just this VM’s resources. If the group is shared, delete the individual resources instead.