VMware Cloud Community
mshorrosh123
Contributor
Contributor
Jump to solution

Sorting vms based on the Folder they are in

I want to sort vm's based on the folder they are in anyone know how to do that? Even better export it to excel sheet.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You want to folder name only or the complete folder path ?

If it's only the folder name you can use a slight variation on the script in .

Something like this

$report = @()

Get-Folder | %{
  $folder = $_.Name
  $_ | Get-VM | %{
    $row = "" | select FolderName, VMName
    $row.FolderName = $folder
    $row.VMName = $_.Name
    $report += $row
  }
}
$report | Sort-Object -Property FolderName | Export-Csv "C:\VM-by-folder.csv" -NoTypeInformation


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

View solution in original post

0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

You want to folder name only or the complete folder path ?

If it's only the folder name you can use a slight variation on the script in .

Something like this

$report = @()

Get-Folder | %{
  $folder = $_.Name
  $_ | Get-VM | %{
    $row = "" | select FolderName, VMName
    $row.FolderName = $folder
    $row.VMName = $_.Name
    $report += $row
  }
}
$report | Sort-Object -Property FolderName | Export-Csv "C:\VM-by-folder.csv" -NoTypeInformation


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

0 Kudos