Isn’t it annoying when you are looking for a VM on your datastore and you cant find it because the folder name is not the same as the VM, use the below script and you will never have that problem again.

This script will compare the name of the VM to the folder it is stored in, any mismatches will be output.

I know a regex expert could probably do this better, let me know if you are one :)

Sample Output:

VM Path
New VM 1 [storage1] New VM
ABCDEV01 [storage1] Dev Test
MicroDS [SAN11] New VM
VM127 [SAN11] Windows 2003 Server

Code:

Connect-VIServer MYVISERVER

$VMFolder = @()
Foreach ($VM in (Get-VM |Get-View)){
$Details = “” |Select-Object VM,Path
$Folder = ((($VM.Summary.Config.VmPathName).Split(])[1]).Split(/))[0].TrimStart( )
$Path = ($VM.Summary.Config.VmPathName).Split(/)[0]
If ($VM.Name-ne $Folder){
$Details.VM= $VM.Name
$Details.Path= $Path
$VMFolder += $Details
}
}
$VMFolder |Export-Csv -NoTypeInformation C:\temp\Mismatch.csv