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
Assuming -temp is part of the name
get-vm | where { $_.powerstate -eq "poweredoff" } | foreach { Set-VM -VM $_ -Name ( ($_.name).Replace("temp", "delete")) }
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?
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.
If a server is powered off with a name of server-temp, I want to rename it to server-delete
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.
Assuming -temp is part of the name
get-vm | where { $_.powerstate -eq "poweredoff" } | foreach { Set-VM -VM $_ -Name ( ($_.name).Replace("temp", "delete")) }
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
The code that DZ1 posted will get rid of the temp with delete.