Deploy and Clean Up a Windows VM in Azure Using PowerShell

To provision a Windows 10 virtual machine in Azure, assign it a public IP address, and successfully connect to it via Remote Desktop Protocol (RDP).

Step-by-Step Process:

1. Azure Login and Subscription Setup

Connect-AzAccount
Set-AzContext -SubscriptionId "<your-subscription-id>"

2. Create Resource Group

New-AzResourceGroup -Name "MyTestRG" -Location "westus"

3. Create Virtual Network and Subnet

$subnetConfig = New-AzVirtualNetworkSubnetConfig -Name "MySubnet" -AddressPrefix "10.0.1.0/24"
$vnet = New-AzVirtualNetwork -Name "MyVNet" -ResourceGroupName "MyTestRG" -Location "westus" -AddressPrefix "10.0.0.0/16" -Subnet $subnetConfig

4. Create Network Security Group with RDP Access

$rdpRule = New-AzNetworkSecurityRuleConfig -Name "Allow-RDP" -Protocol "Tcp" -Direction "Inbound" -Priority 1000 -SourceAddressPrefix "*" -SourcePortRange "*" -DestinationAddressPrefix "*" -DestinationPortRange 3389 -Access "Allow"
$nsg = New-AzNetworkSecurityGroup -Name "MyNSG" -ResourceGroupName "MyTestRG" -Location "westus" -SecurityRules $rdpRule

5. Create Network Interface (NIC)

$subnet = Get-AzVirtualNetworkSubnetConfig -Name "MySubnet" -VirtualNetwork $vnet
$nic = New-AzNetworkInterface -Name "MyNIC" -ResourceGroupName "MyTestRG" -Location "westus" -SubnetId $subnet.Id -NetworkSecurityGroupId $nsg.Id

6. Enter Credentials

$cred = Get-Credential  # Use a username like: azureadmin and a strong password

7. Configure the Windows 10 VM

$vmConfig = New-AzVMConfig -VMName "MyVM" -VMSize "Standard_B1s"
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName "MyVM" -Credential $cred
$vmConfig = Set-AzVMSourceImage -VM $vmConfig -PublisherName "MicrosoftWindowsDesktop" -Offer "Windows-10" -Skus "win10-22h2-pro" -Version "latest"
$vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id

8. Deploy the Virtual Machine

New-AzVM -ResourceGroupName "MyTestRG" -Location "westus" -VM $vmConfig

9. Create and Attach a Public IP Address

$publicIp = New-AzPublicIpAddress -Name "MyPublicIP" -ResourceGroupName "MyTestRG" -Location "westus" -AllocationMethod Static -Sku Basic
$nic = Get-AzNetworkInterface -Name "MyNIC" -ResourceGroupName "MyTestRG"
$nic.IpConfigurations[0].PublicIpAddress = $publicIp
Set-AzNetworkInterface -NetworkInterface $nic

10. Reset VM Admin Credentials (if needed)

Set-AzVMExtension -ResourceGroupName "MyTestRG" -Location "westus" -VMName "MyVM" -Name "ResetAccess" -Publisher "Microsoft.Compute" -ExtensionType "VMAccessAgent" -TypeHandlerVersion "2.4" -Settings @{ "UserName" = "azureadmin" } -ProtectedSettings @{ "Password" = "YourNewP@ssw0rd!" }

Final Step: Connect via Remote Desktop

  1. Launch Remote Desktop (RDP)
  2. Enter the Public IP of your VM
  3. Click “More choices” > “Use a different account”
  4. Login as:
    • Username: azureadmin
    • Password: the one you specified
  5. Accept certificate prompt

You’re now connected! ✅

Clean Up: Delete Azure VM and Resources to Avoid Charges

When you’re done testing, it’s important to clean up to avoid being billed for unused resources like disks, NICs, static IPs, and NSGs. You can do that with one simple PowerShell command:

# Clean up everything in one shot
Remove-AzResourceGroup -Name "MyTestRG" -Force -AsJob

This command deletes:

  • The Virtual Machine (MyVM)
  • Network Interface (MyNIC)
  • Network Security Group (MyNSG)
  • Virtual Network (MyVNet) and Subnet (MySubnet)
  • Public IP (MyPublicIP)
  • Managed Disks and any attached resources

📎 View on GitHub

© 2012–2025 Jet Mariano. All rights reserved.
For usage terms, please see the Legal Disclaimer.

error: Content is protected !!