VMware Cloud Community
SandyB
Enthusiast
Enthusiast
Jump to solution

PowerCLI script to get vm Folders

Hi there

I am looking for a bit of help to script the following senario.

I want to run a script on our production cluster to get a list of VMs and the Folder location in vCenter then export to a csv file.

then at our DR site i want to use that csv file to move the list of VMs to the correct folders.

I have the script below already

Move-VM -VM fileserver1 -Destination (Get-Folder -Name "File Servers")

but would need to have a line per VM that would constantly need updating.

any help much appreciated.

S

Tags (2)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Assuming that all your folders are level 1 folder, in other words not nested folders, you can do the following to create a CSV file

Get-VM | select Name,@{N="Folder";E={$_.Folder.Name}} | `
Export-Csv "C:\vm-folder.csv" -NoTypeInformation -UseCulture

To move the VMs to the correct folder from this CSV file you could do

Import-Csv "C:\vm-folder.csv" -UseCulture | %{
    Move-VM -VM $_.Name -Destination (Get-Folder -Name $_.Folder)
}


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

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

Assuming that all your folders are level 1 folder, in other words not nested folders, you can do the following to create a CSV file

Get-VM | select Name,@{N="Folder";E={$_.Folder.Name}} | `
Export-Csv "C:\vm-folder.csv" -NoTypeInformation -UseCulture

To move the VMs to the correct folder from this CSV file you could do

Import-Csv "C:\vm-folder.csv" -UseCulture | %{
    Move-VM -VM $_.Name -Destination (Get-Folder -Name $_.Folder)
}


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

SandyB
Enthusiast
Enthusiast
Jump to solution

Hi Luc

the command you gave works perfectly even for nested folders as long as all folder names are unique.

many thanks

S

Reply
0 Kudos
hespino2
Contributor
Contributor
Jump to solution

Muchas gracias, me sirvio mucho dicho codigo. Saludos.

Reply
0 Kudos