VMware Cloud Community
jasonpalmquist
Contributor
Contributor

Script to Move VMs to Folders by OS

To start off, I have very minimal coding/scripting skills. However, I am tasked with finding this solution:

I need a script that will go through the VMS in a VCenter, Identify them by the host OS, and then moving them into a specified folder such as "Linux" or " Windows"

thoughts?

0 Kudos
5 Replies
Zsoldier
Expert
Expert

$VMs = Get-VM

Foreach ($VM in $VMs)

{

If ($VM.Extensiondata.guest.guestfamily -match "linux") {Move-VM -VM $VM -Destination (Get-Folder Linux)}

Else {Move-VM -VM $VM -Destination (Get-Folder Windows)}

}

Or something along these lines.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
jasonpalmquist
Contributor
Contributor

somewhat helpful, but just moves everything not linux into the Windows Folder, when it just needs to move the windows machines into the windows folder

0 Kudos
Zsoldier
Expert
Expert

Then just do this:

$VMs = Get-VM

Foreach ($VM in $VMs)

{

If ($VM.Extensiondata.guest.guestfamily -match "windows") {Move-VM -VM $VM -Destination (Get-Folder Windows)}

}

You can add more if statements if you want to move different OS's into different folders too.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
jasonpalmquist
Contributor
Contributor

Can we make it so in only looks in the "Discovered Virtual Machine folder" and not the whole inventory?

0 Kudos
LucD
Leadership
Leadership

Try something like this

$VMs = Get-Folder -Name "Discovered Virtual Machine folder" | Get-VM
Foreach ($VM in $VMs){
 
If ($VM.Extensiondata.guest.guestfamily -match "windows") {
   
Move-VM -VM $VM -Destination (Get-Folder Windows)
  }
}


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

0 Kudos