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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: