As part of a migration from one vCenter to another, I wanted to recreate the same VMs and Templates look which meant recreating the folders. This is VERY slow in the vSphere Web Client and only slightly less tedious in the C# client, so I thought I’d use PowerCLI to do the trick. Here is the folder structure I wished to recreate:
Location -Development -Pending DeComm -Production -Active Directory Domain Controllers -Linux Servers -RDP Servers -Templates -Template VMs
PowerCLI has a cmdlet called New-Folder. Unfortunately, it only creates folders of the Hosts and Clusters type. To create a new VMs and Templates folder, we have to use the Get-View cmdlet. By viewing a datacenter or folder and filtering on the proper location, we can call methods to create a folder with the right context:
(Get-View (Get-View -viewtype datacenter -filter @{"name"="Location"}).vmfolder).CreateFolder("Development") (Get-View -viewtype folder -filter @{"name"="Production"}).CreateFolder("Active Directory Domain Controllers")
The inner Get-View looks at the viewtype datacenter and filters for the name of the specified datacenter. The second example looks at an folder instead of a datacenter, which is useful for nesting folders. Be aware that the “name”=… match is not an exact match – if you had “Location” and “Location2”, or “Location Three”, it would return two objects. You must be precise in your search terms. You can temporarily rename your existing folders to have a unique name, run these commands, and then rename them. You would do that with the Set-Folder command:
Set-Folder -Folder “Production Location” -Name “Production”
With that in mind, creating our folder layout is very simple, though it does require some copy and paste followed by filling out the specifics. Here is what I needed to do to replicate the layout above, including creating some unique folder names that were renamed afterward:
(Get-View (Get-View -viewtype datacenter -filter @{"name"="Location"}).vmfolder).CreateFolder("Development") (Get-View (Get-View -viewtype datacenter -filter @{"name"="Location"}).vmfolder).CreateFolder("Pending DeComm") (Get-View (Get-View -viewtype datacenter -filter @{"name"="Location"}).vmfolder).CreateFolder("Production Location") (Get-View -viewtype folder -filter @{"name"="Production Location"}).CreateFolder("Active Directory Domain Controllers") (Get-View -viewtype folder -filter @{"name"="Production Location"}).CreateFolder("Linux Servers") (Get-View -viewtype folder -filter @{"name"="Production Location"}).CreateFolder("RDP Servers") (Get-View (Get-View -viewtype datacenter -filter @{"name"="Location"}).vmfolder).CreateFolder("Templates Location") (Get-View -viewtype folder -filter @{"name"="Templates Location"}).CreateFolder("Template VMs") Set-Folder -Folder "Production Location" -Name "Production" Set-Folder -Folder "Templates Location" -Name "Templates"
If you want to eliminate the cut and paste, you could create a CSV file with fields for Name, Parent, ParentType and perform one of the two actions based on ParentType. Since I don’t need to do this everyday, that seemed overkill, but I’d love to see what people come up with!
Your script to create the top/parent folder worked great for me, but the nested script did not. I ended up using a much more simple script for the nested/sub folders:
Get-Folder -Name “ParentFolder” | New-Folder -Name “SubFolder”
> To create a new VMs and Templates folder, we have to use the Get-View cmdlet
You wouldn’t believe how long I tried to find that in the vSphere doco and reference for `New-Folder`.
Thanks for the examples on creating VM folders.
Thank you, exactly what I was looking for. Spent the last 20 minutes trying to work out if I was missing something or doing something silly.