How I Deployed and Cleaned Up a Windows Server VM in Azure with PowerShell

 The Problem

After spinning up a few test VMs in Azure, I realized the costs were creeping up from unused resources. Manual cleanup was time-consuming, especially when tracking which NICs, disks, and IPs belonged to what. I needed a faster, cleaner solution—one script to deploy, another to destroy.

 The Solution

I wrote two PowerShell scripts:

  1. New-AzServerVmRdp-20250509_GitHub.ps1: Fully automates deployment of a Windows Server 2019 VM with RDP access.
  2. Remove-AzServerVmRdp-20250509_GitHub.ps1: Cleans up the entire environment by removing the resource group.

These scripts not only deploy the VM but configure the VNet, subnet, NSG, public IP, and NIC—all with one command.

 The Code

Deploy Script:

New-AzResourceGroup -Name "MyTestRG" -Location "westus"
# ... other setup commands
New-AzVM -ResourceGroupName "MyTestRG" -Location "westus" -VM $vmConfig

Cleanup Script:

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

 The Result

I can now spin up a fresh, RDP-ready Windows Server in under 10 minutes and wipe it clean with a single line. I tested the RDP connection, confirmed the VM’s performance, and removed the environment—no residual charges, no clutter.

 🔗 View on GitHub

© 2012–2025 Jet Mariano. All rights reserved.

For usage terms, please see the Legal Disclaimer.

error: Content is protected !!