VMware Cloud Community
tjw82
Contributor
Contributor
Jump to solution

rename multiple VM's based on power state & VM Name

I am looking for a powershell script to rename vm's based on power state and vm name.  For example, I have several vm's with a -temp at the end of server name.  Some are powered on and some are powered off.  I am wanting to rename just the ones that are powered off

Thanks,

tjw82

Reply
0 Kudos
1 Solution

Accepted Solutions
DZ1
Hot Shot
Hot Shot
Jump to solution

Assuming -temp is part of the name

get-vm | where { $_.powerstate -eq "poweredoff" } | foreach { Set-VM -VM $_ -Name ( ($_.name).Replace("temp", "delete")) }

View solution in original post

7 Replies
mark7five7
Contributor
Contributor
Jump to solution

Can you give me an example of what you would like them renamed to?

For example if Server01-temp is poweredoff what would you like the new name to be? 

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

It would be something like this:

get-vm | where { $_.powerstate -eq "poweredoff" } | foreach { Set-VM -VM $_ -Name ("SomeNewName") }

But you didn't specify what the names would be. 

tjw82
Contributor
Contributor
Jump to solution

If a server is powered off with a name of server-temp, I want to rename it to server-delete

Reply
0 Kudos
mark7five7
Contributor
Contributor
Jump to solution

so far I have this:

$vmlist = @(get-vm -name *temp | where {$_.powerstate -match 'PoweredOff'})

foreach ($vm in $vmlist)

    {

        $newname = $vm.name + '-delete'

        Set-VM -name $newname

    }

However this code will set the server name to Server01-Temp-Delete

Let me try to figure out how to do the replace.

DZ1
Hot Shot
Hot Shot
Jump to solution

Assuming -temp is part of the name

get-vm | where { $_.powerstate -eq "poweredoff" } | foreach { Set-VM -VM $_ -Name ( ($_.name).Replace("temp", "delete")) }

tjw82
Contributor
Contributor
Jump to solution

Awesome feedback...

If the -temp can be dropped that would be great.  If not, I can deal with it

I appreciate the help

Thx

tjw82

Reply
0 Kudos
mark7five7
Contributor
Contributor
Jump to solution

The code that DZ1 posted will get rid of the temp with delete.