VMware Cloud Community
ep7network7
Contributor
Contributor

Delete VM Based on VM Name

I am trying to write a PowerCLI script that will delete a VM based on it's naming convention. For example, the VM name is 'DTLA_exp_del_2021.09.13', what would be the best approach to accomplish this task.

Any help is greatly appreciated, thank you.

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

If you want an exact match, I would opt for the match operator.
This allows using a RegEx expression with the anchors ^ and $

Make sure to use the Escape method to 'escape' the special characters in the name, like the . (dot) in your example.

$vmName = 'DTLA_exp_del_2021\.09\.13'

Get-VM | Where-Object {$_.Name -match [RegEx]::Escape("^$($vmName)$"} |
Remove-VM -Confirm:$false -DeletePermanently

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ep7network7
Contributor
Contributor

Thank you for replying to my question. If I decided to adjust the script to remove a VM that is grater than 30 days, would this be a correct approach-

Get-Content c:\temp\servers.txt | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item -Recurse -Force

 

Reply
0 Kudos
LucD
Leadership
Leadership

Not sure what you are trying to do?
Removing files?
That definitely does not remove VMs.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ep7network7
Contributor
Contributor

What I'm trying to do is delete VMs that are powered off that have a specific naming convention. For example, if the VM is named 'DTLA_exp_del_2021.2.9' and it's greater than 30 days then the script will delete the powered-off VM. 

I'm not trying to delete files, only VM's that are older than 30 days.

Reply
0 Kudos
LucD
Leadership
Leadership

The Remove-Item cmdlet is definitely for removing VMs, you have to remove the Remove-VM cmdlet for that.
As I showed in my earlier snippet.

Also, a VM object, which is returned by Get-VM, does not have a LastWriteTime property.
Such  a VM object, since vSphere 6.7, does have a CreateDate property that you could use.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ep7network7
Contributor
Contributor

Since VM object does not have a LastWriteTime property. How can I delete VMs based on it's actual name. For example, I ran command below and stored the output into a variable named $vNames -

$vNames = Get-VM | Where-Object{$_.Name -like "*exp_del*"}

I then ran this command - 

$vmsEXP = $vNames | %{$_.Name} 

When I run $vmsEXP, the output is below -

DTLA_exp_del_2021.03.13
DTLA_exp_del_2020.02.7
DTLA_exp_del_2021.01.17
DTLA_exp_del_2020.08.03
DTLA_exp_del_2020.12.11

Keep in mind there is over 70 VMs with different date ranges. What I'm trying to accomplish is to delete the VMs that are 30 days old based on the VM name. Do I need to place $vmsEXP into an Array and some how figure out a way to remove the VMs. I'm not sure and I'm stuck at this point since VM object does not have a LastWriteTime property.

 

 

 

Reply
0 Kudos
LucD
Leadership
Leadership

You could do something like this

$tgtDate = (Get-Date).AddDays(-30)

Get-VM | Where-Object{$_.Name -like "*exp_del*"} |
ForEach-Object -Process {
    if([DateTime]($_.Split('_')[-1]) -le $tgtDate){
        Remove-VM -VM $_ -Confirm:$false
    }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos