How-To: Delete a VM via a PowerCLI Scheduled Task
This is a PowerCLI script that you can use to delete a VM which can be put into a Windows scheduled task job to be ran at a specified time and date. Below is the code snippet for the script:
Add-PSSnapin VMware*
$VISRV = “vc.lab.local”
$dieVM = “VM-001”
Connect-VIServer $VISRV
$VMToDelete = Get-VM $dieVM
If ($VMToDelete.PowerState –eq “PoweredOn”) {
Shutdown-VMGuest –VM $dieVM
}
Remove-VM $dieVM –DeletePermanently –confirm:$false –RunAsync
To set a scheduled task by PowerCLI in Windows you can follow the well-known Alan Renouf’s walkthrough called Running a PowerCLI Scheduled Task
