PowerCLI Script to Find and Remove Unused Virtual Disks and ISOs
VMware News, virtual machine, vm, VMwareIntroduction
Over time, environments accumulate unused virtual disks and mounted ISOs that consume critical datastore space. Identifying and removing them manually is time-consuming and error-prone. PowerCLI allows you to detect these stale resources and clean them up automatically or in a controlled review process.
In this article, you will learn to:
- Identify VMs with multiple virtual disks
- Find disks not attached to a VM (orphaned VMDKs)
- Unmount ISO files from VM CD drives
- Export cleanup candidates to a review CSV
- Perform safe disk and ISO removal operations
Step 1: Find VMs with More Than One Virtual Disk
Get-VM | Where-Object {
($_ | Get-HardDisk).Count -gt 1
} | Select Name, @{N="DiskCount";E={($_ | Get-HardDisk).Count}}
Export to CSV:
Get-VM | Where-Object {
($_ | Get-HardDisk).Count -gt 1
} | Select Name, @{N="DiskCount";E={($_ | Get-HardDisk).Count}} | Export-Csv "C:ReportsVMs_Multiple_Disks.csv" -NoTypeInformation
Step 2: Detect Unused or Orphaned VMDK Files
List all VMDK files on a datastore:
Get-Datastore -Name "Datastore1" | Get-ChildItem -Recurse | Where-Object {$_.Name -like "*.vmdk"}
Compare that list with in-use VMDKs from Get-HardDisk. Anything left may be orphaned. You can script this into a hash comparison for automation, or review manually.
Step 3: Identify Mounted ISO Files
Get-VM | Get-CDDrive | Where-Object {$_.IsoPath -ne $null} | Select Parent, IsoPath
Unmount all ISO files:
Get-VM | Get-CDDrive | Where-Object {$_.IsoPath -ne $null} | Set-CDDrive -NoMedia -Confirm:$false
Step 4: Export Potential Cleanup Items
$isoMounts = Get-VM | Get-CDDrive | Where-Object {$_.IsoPath -ne $null} | Select Parent, IsoPath
$isoMounts | Export-Csv "C:ReportsMounted_ISOs.csv" -NoTypeInformation
Optionally, document high disk count VMs:
Get-VM | Where-Object {
($_ | Get-HardDisk).Count -gt 3
} | Select Name, PowerState, @{N="DiskCount";E={($_ | Get-HardDisk).Count}} | Export-Csv "C:ReportsDiskAudit.csv" -NoTypeInformation
Diagram: Virtual Disk and ISO Cleanup Flow

Step 5: (Optional) Remove Orphaned VMDKs
Warning: Use caution. Always review manually or verify backups.
Remove-Item -Path "[Datastore1] VMName/VMName_2.vmdk" -Confirm:$false
For better safety, list file and size only:
Get-Datastore -Name "Datastore1" | Get-ChildItem -Recurse | Where-Object {$_.Name -like "*.vmdk"} | Select Name, DatastoreFullPath, Length
Use Case: Monthly Storage Reclamation Job
- Identify all ISOs still mounted
- Export list of VMs with 3+ disks
- List all VMDK files not attached to powered-on VMs
- Unmount ISOs
- Schedule reports to email storage admins
Troubleshooting
| Issue | Solution |
|---|---|
| CD-ROM remains mounted after script | Use -NoMedia and confirm VMware Tools is working |
| Cannot delete VMDK | File may be locked or still attached to an active snapshot |
| Get-ChildItem fails on datastore | Ensure datastore browser access is enabled and PowerCLI has permissions |
| VMs not reporting IsoPath | Requires up-to-date VMware Tools for guest metadata |
The post PowerCLI Script to Find and Remove Unused Virtual Disks and ISOs appeared first on Digital Thought Disruption.