Table of Contents
The Azure PowerShell module is used to create and manage Azure resources from the PowerShell command line or in scripts. This quickstart shows you how to use the Azure PowerShell module to deploy a virtual machine (VM) in Azure.
Connect to Microsoft Azure PowerShell
First, you need connect to Microsoft Azure PowerShell, there’re two ways to run PowerShell commands in Microsoft Azure:
Method 1: From Azure PowerShell modules installed in a computer.

Method 2: From Azure Cloud Shell using browsers.

Create a new Windows VM in Azure using PowerShell
Declare variables then create a new Azure VM using the following script. Don’t forget change the variables to fit with yours.
#Define the following parameters for the virtual machine.
$vmAdminUsername = "bonguides"
$vmAdminPassword = ConvertTo-SecureString "Pass@ord123" -AsPlainText -Force
$ComputerName = "vm-001"
#Define the following parameters for the Azure resources.
$Location = "EastUS"
$ResourceGroupName = "EastUS-RG"
$VMName = "vm-001"
$OsDiskName = "vm-001-OS"
$StorageAccountType = "Premium_LRS"
$VMSize = "Standard_B2s"
#Define the networking information.
$NICName = "vm-001-NIC"
$PublicIpName = "vm-001-IP"
#Define the new VNet information.
$NetworkName = "EastUS-vNet"
$SubnetName = "default"
$SubnetAddressPrefix = "10.0.0.0/24"
$VnetAddressPrefix = "10.0.0.0/16"
$SecurityGroupName = "EastUS-vNet-SG"
#Define the VM marketplace image details.
$PublisherName = "microsoftwindowsdesktop"
$VMOffer = "Windows-10"
$Skus = "21h1-pro"
#Create a new resource group.
New-AzResourceGroup -Name $ResourceGroupName -Location $Location
#Creare and configure new virtual network.
$SingleSubnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix -WarningAction silentlyContinue
$Vnet = New-AzVirtualNetwork -Name $NetworkName -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix $VnetAddressPrefix -Subnet $SingleSubnet
New-AzNetworkSecurityGroup -Name $SecurityGroupName -ResourceGroupName $ResourceGroupName -Location $Location
#Get the subnet details for the specified virtual network + subnet combination.
$VnetSubnet = (Get-AzVirtualNetwork -Name $NetworkName -ResourceGroupName $ResourceGroupName).Subnets | Where-Object {$_.Name -eq $SubnetName}
#Create the public IP address.
$PublicIp = New-AzPublicIpAddress -Name $PublicIpName -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod Dynamic -WarningAction silentlyContinue
#Create the NIC and associate the public IpAddress.
$azureNIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $VnetSubnet.Id -PublicIpAddressId $PublicIp.Id
$nic = Get-AzNetworkInterface -ResourceGroupName $ResourceGroupName -Name $NICName
$nsg = Get-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName -Name $SecurityGroupName
$nic.NetworkSecurityGroup = $nsg
$nic | Set-AzNetworkInterface
#Store the credentials for the local admin account.
$vmCredential = New-Object System.Management.Automation.PSCredential ($vmAdminUsername, $vmAdminPassword)
#Define the parameters for the new virtual machine.
$VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize $VMSize
$VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $ComputerName -Credential $vmCredential -ProvisionVMAgent -EnableAutoUpdate
$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $azureNIC.Id
$VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName $PublisherName -Offer $VMOffer -Skus $Skus -Version "latest"
$VirtualMachine = Set-AzVMBootDiagnostic -VM $VirtualMachine -Disable
$VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -StorageAccountType $StorageAccountType -Caching ReadWrite -Name $OsDiskName -CreateOption FromImage
#Create the virtual machine.
New-AzVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VirtualMachine -Verbose
5/5 - (1 vote)