PowerCLI Script to Find and Remove Unused Virtual Disks and ISOs

Introduction

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:\Reports\VMs_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:\Reports\Mounted_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:\Reports\DiskAudit.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

  1. Identify all ISOs still mounted
  2. Export list of VMs with 3+ disks
  3. List all VMDK files not attached to powered-on VMs
  4. Unmount ISOs
  5. Schedule reports to email storage admins

Troubleshooting

IssueSolution
CD-ROM remains mounted after scriptUse -NoMedia and confirm VMware Tools is working
Cannot delete VMDKFile may be locked or still attached to an active snapshot
Get-ChildItem fails on datastoreEnsure datastore browser access is enabled and PowerCLI has permissions
VMs not reporting IsoPathRequires up-to-date VMware Tools for guest metadata

Leave a Reply

Discover more from Digital Thought Disruption

Subscribe now to keep reading and get access to the full archive.

Continue reading