VMware Cloud Community
Sundararajan
Enthusiast
Enthusiast

Powecli Scripting to Decommission VM

Hi All,

I want Powercli script to decommission a Windows VM, I have no idea on scripting so anyone please help to share the complete script. Thanks.

0 Kudos
5 Replies
virtualg_uk
Leadership
Leadership

Can you elaborate on what you mean by decommission, ie do you just want to delete the VM and all attached disks or do you want to perform other tasks?


Graham | User Moderator | https://virtualg.uk
0 Kudos
Sundararajan
Enthusiast
Enthusiast

Want to delete the VM and all attached disks from VC.

0 Kudos
virtualg_uk
Leadership
Leadership

‌You'll just need to use the remove-vm powercli cmdlet with the correct parameters as per: Remove-VM - vSphere PowerCLI Cmdlets Reference

PowerCLI Getting started guide:

http://www.virtualizationadmin.com/articles-tutorials/vmware-esx-and-vsphere-articles/general/gettin...


Graham | User Moderator | https://virtualg.uk
0 Kudos
Sundararajan
Enthusiast
Enthusiast

Hi Grba,

This will not fulfill my requirements , I need a complete script to decommission list of VM on that script.

0 Kudos
virtualg_uk
Leadership
Leadership

Try the following from Powershell: Shutdown VM and Delete From Disk | Brian Gordon but ensure you test before running in a production environment as it will delete data:

Create a list of VMs to delete and store in a file named servers.txt

$VMs = (Get-Content servers.txt)

$vmObj = Get-vm $vms

foreach($active in $vmObj){

if($active.PowerState -eq "PoweredOn"){

Stop-VM -VM $active -Confirm:$false -RunAsync | Out-Null}

}

Start-Sleep -Seconds 7

foreach($delete in $vmObj){

Remove-VM -VM $delete -DeleteFromDisk -Confirm:$false -RunAsync | Out-Null}


Graham | User Moderator | https://virtualg.uk
0 Kudos