Deploy & Remove Windows Server VM in Azure via RDP

Automate the full lifecycle of a Windows Server VM in Azure — from deployment to secure RDP access and safe cleanup — using PowerShell.

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 Public IP Address

$publicIp = New-AzPublicIpAddress -Name "MyPublicIP" -ResourceGroupName "MyTestRG" -Location "westus" -AllocationMethod Static -Sku Basic

6. Create Network Interface

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

7. Enter Credentials

$cred = Get-Credential  # Use a simple username like 'azureadmin'

8. Configure the Server VM

$vmConfig = New-AzVMConfig -VMName "MyServerVM" -VMSize "Standard_B1s"
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName "MyServerVM" -Credential $cred
$vmConfig = Set-AzVMSourceImage -VM $vmConfig -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2019-Datacenter" -Version "latest"
$vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id

9. Deploy the Server VM

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

10. 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. Log in with:
    • Username: azureadmin
    • Password: the one you specified
  5. Accept the certificate prompt

✅ You’re connected!

Clean Up: Delete Azure Windows Server VM and Resources to Avoid Charges

To prevent ongoing charges after testing, it’s important to delete all associated resources, including:

  • The Virtual Machine (MyServerVM)
  • Public IP Address
  • Network Interface (MyNIC)
  • Network Security Group (MyNSG)
  • Virtual Network and Subnet (MyVNet, MySubnet)
  • Managed Disk
  • And any other resource under the resource group

You can remove all of these at once using the following command:

Remove-AzResourceGroup -Name "MyTestRG" -Force -AsJob

🔗View on GitHub

© 2012–2025 Jet Mariano. All rights reserved.

For usage terms, please see the Legal Disclaimer.

error: Content is protected !!