I have been playing with this various Orphaned VMKD scripts that I could find and came across the Spring Cleaning one by Luc D.
I only have a couple issue with what I have been attempting to resolve. I need to exclude the .snapshot directory from what is returned and
I need to know is there a way to reslove the datastore VMDK back to a specific host.
Thank you so much for your assistance and make it a great day.
In my script there is already a test to exclude the cos.vmdk and esxconsole.vmdk files.
You could add a similar test to exclude the .snapin folders.
Let me know if you need help with that ?
You want to find back the host, does this mean that the datastore(s) are not shared between ESX(i) hosts ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc D,
As always thank you for what you add. I just needed to get the jumble of coding languages straight in my head. I am testing adding a -or option to eliminate the .snapin folders.
I am fairly certain that the datastore(s) are shared between the ESXi hosts but I will validate this just in case there are any exceptions.
Thank you once again for your wealth of knowledge.
The script is working but I am not excluding the .snapshot path as I excpected to.
Is this because I need to and the if statements?
if (-not ($pathAsString.toLower().contains("-ctk.vmdk"))) {
if (-not ($pathAsString.toLower().contains(".snapshot")))
{
I'm not sure how you got the content of the $pathAsString variable, but try the test like this
if ($pathAsString -notmatch "-ctk.vmdk" -and $pathAsString -notmatch ".snapshot") {
There is no need to convert to lowercase, this is case-insentive
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
For some reason the .snapshot/ path is not being eliminated -
-ProxyPolicy Noproxy
# Also detects VMDKs from machines that need snapshot consolidation (from differentials that exist but are not part of the tree).
# Author: HJA van Bokhoven
# Modifications: LucD
= @()
$arrUsedDisks = Get-View -ViewType VirtualMachine | % {$_.Layout} | % {$_.Disk} | % {$_.DiskFile}
$arrDS = Get-Datastore | Sort-Object -property Name
foreach ($strDatastore in $arrDS) {
Write-Output "$($strDatastore.Name) Orphaned Disks:"
$ds = Get-Datastore -Name $strDatastore.Name | % {Get-View $_.Id}
$fileQueryFlags = New-Object VMware.Vim.FileQueryFlags
$fileQueryFlags.FileSize = $true
$fileQueryFlags.FileType = $true
$fileQueryFlags.Modification = $true
$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.details = $fileQueryFlags
$searchSpec.matchPattern = "*.vmdk"
$searchSpec.sortFoldersFirst = $true
$dsBrowser = Get-View $ds.browser
$rootPath = "[" + $ds.Name + "]"
$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
foreach ($folder in $searchResult)
foreach ($fileResult in $folder.File)
if ($fileResult.Path)
$pathAsString = out-string -InputObject $FileResult.Path
if (-not ($arrUsedDisks -contains ($folder.FolderPath + $fileResult.Path))){
# Changed Black Tracking creates ctk.vmdk files that are not referenced in the VMX. This prevents them from showing as false positives.
if ($pathAsString -notmatch "-ctk.vmdk" -and $pathAsString -notmatch "snapshot") {
$row = "" | Select DS, Path, File, Size, ModDate
$row.DS = $strDatastore.Name
$row.Path = $folder.FolderPath
$row.File = $fileResult.Path
$row.Size = $fileResult.FileSize
$row.ModDate = $fileResult.Modification
$report += $row
$report | Export-Csv C:\Scripts\OrphanedVMDKs.csv
Write-Output "$($row.Path)$($row.File)"