tjw82
Contributor
Contributor

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

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
DZ1
Hot Shot
Hot Shot

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

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? 

0 Kudos
DZ1
Hot Shot
Hot Shot

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

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

0 Kudos
mark7five7
Contributor
Contributor

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

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

tjw82
Contributor
Contributor

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

0 Kudos
mark7five7
Contributor
Contributor

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