
Setting up folders to organize objects in vCenter
vSphere folders are flexible containers with other vSphere objects inside. Folder objects in vSphere are meant to be a logical organizational structure for objects that are not tied to physical resources. This means that VM objects from different clusters or even different datacenters can be logically grouped together. The same applies to port groups, switches, or datastores.
This is important as you begin to look at delegating access from VMs to operators, developers, and other users in the organization, so that you can group together all of the VMs that a user needs to access. Folders also help administrators to easily locate objects and report on objects for a particular business unit or group within their companies.
In this recipe, you will look at the simple cmdlets used to create folder structures in vSphere, and move objects into these folders with simple PowerCLI cmdlets.
Getting ready
To begin, you need to open a PowerCLI window and connect to a vCenter server. You should also read and review the Creating a virtual datacenter in vCenter recipe earlier in this chapter, since it discusses the hierarchy of folders inside a vSphere datacenter. This recipe uses a lot of the concepts introduced in the earlier recipes.
For this recipe, you will use the New-Folder
cmdlet and understand the different types of folders that it can create inside vCenter. Folders are used in multiple areas of vCenter for organizational purposes. You will also take a look at the use of Get-Folder
and Remove-Folder
.
The New-Folder
cmdlet is another cmdlet that relies on the -Location
parameter to determine where to create the object you're defining. As you observed in the Creating a virtual datacenter in vCenter recipe, you are able to use the Get-Folder
cmdlet to return the four special folders automatically provisioned under a datacenter object.
For this example, you will create several folder structures. You will create two, two-level folders under the VM and Templates view for Infrastructure and App Servers. You will create two subfolders called Domain Controllers and VMware under Infrastructure. You will create a Standard vSwitches folder in the Networks view and you will create an NFS and iSCSI folder under the Datastores view. Finally, you will create a Finance and IT folder under the Host and Clusters view to store clusters owned by these businesses. The following illustrates this structure:

How to do it…
In order to set up folders to organize objects in vCenter, perform the following steps:
- The first step is to retrieve the datacenter where you want these folders to be created. By first getting the datacenter object, you ensure that if you had more than one datacenter defined, you would be operating in the correct datacenter. So, the first step is to run
Get-Datacenter
and find ourPrimary
datacenter:Get-Datacenter -Name "Primary"
- The next step is to pipe this datacenter object into a
New-Folder
cmdlet, which will limit the results to within this datacenter. If you start with theInfrastructure
folder, you need to return the root folder with typevm
:Get-Datacenter -Name "Primary" | Get-Folder -name "vm"
- Now that you have a single folder, this will serve as our location parameter for our new
Infrastructure
folder. UsingNew-Folder
, you will pass in our desired name and the location parameter from the previous step:New-Folder -Name "Infrastructure" -Location (Get-Datacenter -Name "Primary" | Get-Folder -name "vm")
- Next, you can repeat the same step with our
App Servers
folder. The only parameter that should change is the name parameter:New-Folder -Name "App Servers" -Location (Get-Datacenter -Name "Primary" | Get-Folder -name "vm")
- The next step is to create a subfolder under Infrastructure for Domain Controllers. To do this, you change the name and the location of the same cmdlet. Instead of searching for the folder named
vm
, you will search for the one you just created namedInfrastructure
. While we're at it, you can create theVMware
folder as well by repeating the cmdlet with a different name defined:New-Folder -Name "Domain Controllers" -Location (Get-Datacenter -Name "Primary" | Get-Folder -name "Infrastructure") New-Folder -Name "VMware" -Location (Get-Datacenter -Name "Primary" | Get-Folder -name "Infrastructure")
- Next, you will move to create the
Standard vSwitches
folder under the Networking area. To do this, you need to run theNew-Folder
cmdlet and search for the rootnetwork
folder in the datacenter for the location:New-Folder -Name "Standard vSwitches" -Location (Get-Datacenter -Name "Primary" | Get-Folder -Name "network")
- With this successfully created, you will write three additional
New-Folder
cmdlets creating theNFS
andiSCSI
datastore folders and theFinance
andIT
host folders. These follow the same format:New-Folder -Name "NFS" -Location (Get-Datacenter -Name "Primary" | Get-Folder -Name "datastore") New-Folder -Name "iSCSI" -Location (Get-Datacenter -Name "Primary" | Get-Folder -Name "datastore") New-Folder -Name "Finance" -Location (Get-Datacenter -Name "Primary" | Get-Folder -Name "host") New-Folder -Name "IT" -Location (Get-Datacenter -Name "Primary" | Get-Folder -Name "host")
- With all of the folder structures now created, you can take a look at moving objects into these locations. You will use multiple cmdlets that begin with
Move-
in order to relocate objects into these folders you have created. Let's begin with the VM folderVMware
, and relocate our vCenter server into that folder. Since you know that this is the only folder namedVMware
in vCenter, you will use the shortcut and just useGet-Folder
with the name:Move-VM -VM "vCenterSrv" -Location (Get-Folder -Name "VMware")
- The next object you want to relocate is the cluster you created named
BigCluster
using theMove-Cluster
cmdlet.Move-Cluster
requires the-Location
parameter and also a cluster name with the-Cluster
parameter. You can relocateBigCluster
into theIT
host folder:Move-Cluster -Cluster "BigCluster" -Location (Get-Folder -Name "IT")
- Lastly, you can reorganize our datastores logically using the
Move-Datastore
cmdlet. This cmdlet uses the parameter-Destination
instead of-Location
, but accepts the input of aFolder
object:Move-Datastore -Datastore "NFSDatastore1" -Destination (Get-Folder -Name "NFS") Move-Datastore -Datastore "iSCSIDatastore1" -Destination (Get-Folder -Name "iSCSI")
- Unfortunately, there is no native cmdlet to move
PortGroups
fromStandard vSwitches
into folders in the Networking view, but it can be done in the vSphere Client GUI.
How it works…
The New-Folder
cmdlet automatically determines what type of folder to create based on the location passed to it. There are five types of folders: datacenter, vm, host, network, and datastore. If you pass the vm folder location, a vm folder will be created. The type of folder determines where the folder is visible in the GUI. In PowerCLI, you can also use these types to scope the results returned from the Get-Folder
cmdlet by passing the -Type
parameter.
If you can see, each of the Move-
cmdlets accepts a name parameter that is specific to the type of object it expects: Move-VM
uses the -VM
parameter, Move-Datastore
uses -Datastore
, and Move-Cluster
uses the -Cluster
parameter. This follows a logical pattern that you can expect even without using Get-Help
to see instructions.
There's more…
In this recipe, you worked with host, vm, network, and datastore groups within a datacenter. vSphere also allows you to create datacenter folders at the same level as datacenter objects to arrange datacenters logically.
See also
- The Deploying new virtual machines from a template and Creating basic reports of VM properties using VMware Tools and PowerCLI recipes in Chapter 3, Managing Virtual Machines